I'm having an issue with following code.console.log
is working fine in logName
method but not giving desired output in logNameAgain
method. I tried to google but in vain. Could someone please explain what is going on here?
var testObj = {
name: "This is test object",
logName: function() {
console.log(this.name); //Works fine
function logNameAgain() {
console.log(this.name); //Not giving expected result;
}
logNameAgain();
}
};
testObj.logName();
Seems like console.log
in logNameAgain
method pointing to window
. It doesn't really make any sense to me?
Update: I understand that this can be fixed by using bind/call or self but I don't understand why this is happening?