Why here "this" is different for self executing code(window/globalScope), inside a object's member function?
foo = "Window_foo"
var myobj = {
foo: 'Object_bar',
func: function(){
var self = this;
console.log("outer this.foo " + this.foo);
console.log("outer self.foo " + self.foo);
(function(){
console.log("inner this.foo " + this.foo);
console.log("inner self.foo " + self.foo);
}());
}
}
myobj.func();
I get below output:
outer this.foo Object_bar
outer self.foo Object_bar
inner this.foo Window_foo
inner self.foo Object_bar
func is the member function of myObj object. "this" inside it must be the myObj object. Then how self executing function inside it has "this" as window object?