这个题目大概考察3个点
1. 匿名函数中this指针的指向
2. 自执行函数
3. 闭包
obj = {
show: function() {
console.log(this);
},
make: function() {
return this;
}
}
可以推到
(obj.make) = (function(){return this;})
(obj.make = obj.make) = (obj.make = function(){return this;}) = (function(){return this;})
所以看起来 (obj.make) = (obj.make = obj.make) = (function(){return this;})
所以 (function(){return this;})() 怎么看都像是一个闭包 this 指向 window
但是执行的时候
其实
(obj.make)() = (obj.make()) = obj.make(); //更像是自执行函数
而
(obj.make = obj.make)() = (function(){return this;})(); //更像是闭包
可见 (obj.make)() 中的 this 指向了 obj 。。 这个如何理解
1. 匿名函数中this指针的指向
2. 自执行函数
3. 闭包
obj = {
show: function() {
console.log(this);
},
make: function() {
return this;
}
}
可以推到
(obj.make) = (function(){return this;})
(obj.make = obj.make) = (obj.make = function(){return this;}) = (function(){return this;})
所以看起来 (obj.make) = (obj.make = obj.make) = (function(){return this;})
所以 (function(){return this;})() 怎么看都像是一个闭包 this 指向 window
但是执行的时候
其实
(obj.make)() = (obj.make()) = obj.make(); //更像是自执行函数
而
(obj.make = obj.make)() = (function(){return this;})(); //更像是闭包
可见 (obj.make)() 中的 this 指向了 obj 。。 这个如何理解