为什么第二个函数没有使用"use strict";模式(在控制台中显示窗口对象):
function test() {
console.log(this);
}
test(); // will be global or window, it's okay
"use strict";
function test2() {
console.log(this);
}
test2(); // will be global, BUT WHY? It must be undefined, because I have used strict mode!
如果我在第二个函数的主体中定义了严格模式,那么一切都会如预期一样。
function test() {
console.log(this);
}
test(); // will be global or window
function test2() {
"use strict";
console.log(this);
}
test2();
我的问题很简单——为什么会发生这种情况?
"use strict";
必须位于代码块或函数的最顶部。如果它之前有任何代码,则不起作用。 - Pointy