Daily-Question
Daily-Question copied to clipboard
【Q574】关于 this,判断以下代码输出
function foo() {
console.log( this.a );
}
var a = 2;
(function(){
"use strict";
foo();
})();
输出: 2
只有在存在 this 的函数中设置严格模式,this 为 undefined。因此此时会正常输出。
山月老师,应该是“只有在存在 this 的函数前设置严格模式,this为undefined。”才对。
"use strict";
function foo() {
console.log( this.a );
}
var a = 2;
(function(){
foo();
})();
按以上代码执行,函数foo
中的this
就不能指向window
了。
不应该只是在函数中,在函数前设置严格模式,this
为undefined