因为这里实际上没有范围。所有this
访问都引用该window
对象。因此,当您this.myRefVar
在内部范围内进行编辑时,您实际上是在编辑window
.
var theName = "SO";
var myObject = function(){
this.theName = "SO2";
this.foo = function() {
this.theName = "SO3";
}
}
在这里,我定义了一些变量和函数。变量theName
,首先声明在root(window)
作用域,然后在myObject
作用域内(没有这样的作用域,只是为了解释,然后在foo
作用域内。)
console.log(theName); // SO
console.log(this.theName); // SO
console.log(window.theName); // SO
console.log(myObject.theName); // undefined
console.log(myObject.foo); // undefined
console.log(this.foo); // undefined
console.log(window.foo); // undefined
在这里,我试图theName
通过不同的方式访问变量。如果这里实际上有范围,第四个应该在函数调用后工作。其他只是代表相同的想法,但方式不同。
myObject();
console.log(theName); // SO2
console.log(this.theName); // SO2
console.log(window.theName); // SO2
console.log(myObject.theName); // undefined
console.log(myObject.foo); // undefined
console.log(this.foo); // function myObject/this.foo()
console.log(window.foo); // function myObject/this.foo()
函数调用后,我仍然无法myObject.theName
按我希望的那样访问。那是因为,以这种方式调用它myObject.theName
实际上并没有访问myObject
范围,而不是我试图访问函数theName
的属性。myObject
而且,如果没有实际定义/实例化/创建这个函数作为一个对象,我就无法访问这些属性。
myObject.theName;// undefined. Accessing myObject as a function
new myObject().theName // SO2. Accessing an object derived from myObject.
您的代码中发生的实际上不是范围,而是关闭。为了更好地理解:
Scoping
Closures
Similar SO question