var f = function() {
this.x = 5;
(function() {
this.x = 3;
})();
console.log(this.x);
};
var obj = {
x: 4,
m: function() {
console.log(this.x);
}
};
f(); // 3
new f(); // 5
f.call(f); // 5
obj.m.call(f); // 5
If you call the function, the result is 3, but if you bind the context with f.call(f) - the result is 5. Also, if I call the functions as a constructor. Why it happens? Sorry for the stupid question, but I tried to find the answer, but could not.
In the first case, you call
f()as a simple named function. So thethiskeyword inside of it refers to the global scope (the window).Consequence: the
thisinside the function body and thethisinside the closure inside the function body point to the same object: the window.In the second case you call f() as a constructor, so the
thiskeyword will point to the created object. But the closure, as it's anonymous, will keep refering to the global scope withthis, so in this case they don't point to the same thing and the object created by the constructorf()is not mutated by the closure.In the 3rd case, you call
flike the first time but specifying the value ofthisas being the functionfitself. So the behavior ofthishere is equivalent to the second case.In the 4th case, you call
musing the context off, so thethis.xwill refer tofforthis, and the closure insidefkeeps refering to the window.