在这里有一个完整的噩梦,努力向自己解释为什么this是window对象?
(function () {
function get () {
alert(this);
}
get();
})();
Run Code Online (Sandbox Code Playgroud)
我的理解是函数在JS中创建了范围,它是如何引用windowObject的?
因为这是JavaScript的工作方式,禁止严格模式.
this是window,除非你明确地调用一个方法不同的对象上.您发布的代码get没有显式调用this,因此调用它window.
x.method() // "this" will be "x"
method() // "this" will be "window"
Run Code Online (Sandbox Code Playgroud)