I wonder why the code below returns undefined instead of 5:
var f = function() { this.x = 5; (function() { this.x = 3; })(); console.log(this.x); }; var obj = {x: 4, m: function() { console.log(this.x); }}; obj.m.call(f); I wonder why the code below returns undefined instead of 5:
var f = function() { this.x = 5; (function() { this.x = 3; })(); console.log(this.x); }; var obj = {x: 4, m: function() { console.log(this.x); }}; obj.m.call(f); Feel the difference between
obj.m.call(f); and
obj.m.call(new f); In the first case, you pass the function, and in the second, the object.
this in obj.m will contain a function that can be called like this() :. And since is a function, not an object, then the value of this.x changed only in the passed function f() . Here, probably something like that. - lampaSource: https://ru.stackoverflow.com/questions/263088/
All Articles