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不同
關于this的問題
慕桂英3389331
2018-09-10 09:09:35