JavaScript高级程序设计中有下面这一句话:
在严格模式下,未指定环境对象而调用函数,则 this 值不会转型为 window。 除非明确把函数添加到某个对象或者调用 apply()或 call(),否则 this 值将是 undefined。
我进行了如下的尝试,希望能重点解释setTimeout和全局函数这三个中的this.
"use strict";
(function(){
console.log(this)//undefined
})();
!function(){
console.log(this)//undefined
}();
setTimeout(function(){
console.log(this) //window
},0);
function a(){
console.log(this);
}
a(); //undefined
为什么我打印的跟你的不一样