Can someone explain the concept behind this ?
assume we have a function as a method of an object:
var myobj = {
myfunc : function () {
console.log(this); // Logs reference to myobj.
}
};
myobj.myfunc();
while a nested function inside the same function myfunc
will return reference to head object ( window ) :
var myobj = {
myfunc : function () {
( function (){
console.log(this); // Logs reference to head window object.
})();
}
};
myobj.myfunc();
It really confuses me how this works, as i thought function will always return reference to the parent object.
The question is, is this a fixed rule, that is any nested function will always rreturn reference to window
?