在 JS 中访问外部函数范围时出现问题
issue when access outer function scope in JS
为什么会出现以下情况?
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()();
输出:
parent contains myRefVar,myRefVar2
parent value of myRefVar: 20
this value of myRefVar: 20
因为这里实际上没有范围界定。所有 this
访问都引用 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 at root(window)
scope, then inside myObject
scope (There is no scope like this, just for the explanation, and then inside foo
scope.)
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. If there is actually scopping here 4th one should work after function call. 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. That's because, calling it this way myObject.theName
does not actually accessing myObject
scope, rather than I am trying to access theName
property of myObject
function. 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.
您的代码中发生的实际上不是范围而是关闭。为了更好地理解:
Scopping
Closures
Similar SO question
在JavaScript函数中有全局作用域
例如
function parent() {
var self_parent = this;
function firstChild() {
var self_first_child = this;
function childOfChild() {
var self_child_of_child = this;
}
}
}
在上面的代码中,以下内容为真
self_parent === self_first_child === self_child_of_child
有关详细信息,请参阅 JavaScript-Garden-About-this
为什么会出现以下情况?
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()();
输出:
parent contains myRefVar,myRefVar2
parent value of myRefVar: 20
this value of myRefVar: 20
因为这里实际上没有范围界定。所有 this
访问都引用 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.)
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. If there is actually scopping here 4th one should work after function call. 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. That's because, calling it this waymyObject.theName
does not actually accessingmyObject
scope, rather than I am trying to accesstheName
property ofmyObject
function. 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.
您的代码中发生的实际上不是范围而是关闭。为了更好地理解:
Scopping
Closures
Similar SO question
在JavaScript函数中有全局作用域 例如
function parent() {
var self_parent = this;
function firstChild() {
var self_first_child = this;
function childOfChild() {
var self_child_of_child = this;
}
}
}
在上面的代码中,以下内容为真
self_parent === self_first_child === self_child_of_child
有关详细信息,请参阅 JavaScript-Garden-About-this