[英]issue when access outer function scope in JS
Why the following happens? 为什么会发生以下情况?
function f1() {
this.myRefVar = 30;
this.myRefVar2 = 30;
var parent = this;
return function() {
this.myRefVar = 20;
console.log('parent contains ' + Object.keys(parent).filter(function(k) {
return k.indexOf('myRefVar') > -1;
}));
console.log('parent value of myRefVar: ' + parent.myRefVar);
console.log('this value of myRefVar: ' + this.myRefVar);
};
}
f1()();
Output: 输出:
parent contains myRefVar,myRefVar2
parent value of myRefVar: 20
this value of myRefVar: 20
Because there is actually no scoping here. 因为这里实际上没有作用域。 All of
this
accesses refer to the window
object. 所有
this
访问都引用window
对象。 Hence, when you are editing this.myRefVar
at the inner scope, you are actually editting the value at the window
. 因此,在内部范围内编辑
this.myRefVar
时,实际上是在window
编辑值。
var theName = "SO";
var myObject = function(){
this.theName = "SO2";
this.foo = function() {
this.theName = "SO3";
}
}
Here, I defined some variables, and functions.
在这里,我定义了一些变量和函数。 The variable
theName
, first declared atroot(window)
scope, then insidemyObject
scope (There is no scope like this, just for the explanation, and then insidefoo
scope.)变量
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
And here, I am trying to access
theName
variable via different ways.在这里,我试图通过不同的方式访问
theName
变量。 If there is actually scopping here 4th one should work after function call.如果这里确实存在作用域,则第4个应该在函数调用后起作用。 The others just representing same idea, but different way.
其他人只代表相同的想法,但方式不同。
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()
After function call, I still can't access
myObject.theName
as I hoped.函数调用后,我仍然无法按我希望的方式访问
myObject.theName
。 That's because, calling it this waymyObject.theName
does not actually accessingmyObject
scope, rather than I am trying to accesstheName
property ofmyObject
function.这是因为以这种方式调用
myObject.theName
实际上并不访问myObject
范围,而不是试图访问myObject
函数的theName
属性。 And, without actually defining/instantiating/creating this function as an object, I cannot access the properties.而且,在没有实际定义/实例化/创建此函数作为对象的情况下,我无法访问属性。
myObject.theName;// undefined. Accessing myObject as a function
new myObject().theName // SO2. Accessing an object derived from myObject.
What's going on in your code is actually not scopping but closure. 您的代码中发生的实际上不是作用域而是闭合。 For better understanding:
为了更好的理解:
Scopping 范围界定
Closures 关闭
Similar SO question 类似的问题
In JavaScript function have global scope For example JavaScript函数中具有全局作用域例如
function parent() {
var self_parent = this;
function firstChild() {
var self_first_child = this;
function childOfChild() {
var self_child_of_child = this;
}
}
}
in the above code following will be true 在上面的代码中,以下将为真
self_parent === self_first_child === self_child_of_child
for more Info see JavaScript-Garden-About-this 有关更多信息,请参见JavaScript-花园-关于此
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.