function foo() { "use strict"; console.log( this.a );
}var a = 2;
foo();使用严格模式,默认绑定无法到达全局,出现undefined这个可以理解但是用一个匿名函数就: function foo() {
console.log( this.a );
} var a = 2;
(function(){ "use strict";
foo(); // 2
})();这要如何理解,这里的this不是应该指向匿名函数?调用栈不是全局=>匿名函数=>foo?补充:疑惑在于为什么第二种写法会是那样的结果。备注:讨论只局限在es3范畴,请不要用胖箭头它的this指向和es3不同
1 回答
元芳怎么了
TA贡献1798条经验 获得超7个赞
var a = 2; function foo() {
"use strict"; console.log( this.a );
}
(function(){
foo(); // 报错
})();不应该是这样吗
或者这样?
var a = 2;
(function () { "use strict"; (()=> { console.log(this.a);
})(); // 报错})();添加回答
举报
0/150
提交
取消
