简体   繁体   English

在JS中访问外部函数作用域时的问题

[英]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 at root(window) scope, then inside myObject scope (There is no scope like this, just for the explanation, and then inside foo 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 way myObject.theName does not actually accessing myObject scope, rather than I am trying to access theName property of myObject 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.

相关问题 d3.js:使用function(d){…}时如何访问外部范围的数据 - d3.js: How to access data in outer scope when using function(d) {…} 如何从内部函数访问外部作用域? - How to access outer scope from inner function? Javascript:使用'this'访问'private'子函数中的外部范围变量时得到'undefined' - Javascript: got 'undefined' when using 'this' to access outer scope variable in 'private' child function 在全局范围内访问JS函数 - Access JS Function in Global Scope 如何在HighCharts中的单击函数中访问外部范围变量 - How to access outer scope variables within a click function in HighCharts 如何从 JS 模块 class 中调用外部 scope function - How to call an outer scope function from within a JS module class JS:进行闭包时,如果我不存储为变量,内部函数如何访问外部函数参数? - JS: When making a closure, how does the inner function access the outer function argument if I'm not storing as a variable? Javascript外部范围变量访问 - Javascript outer scope variable access Node.js EventEmitter函数错误,当typeof返回函数时,侦听器必须是函数-可能是范围问题 - Node.js EventEmitter function error `listener must be a function` when `typeof` returns function - could be scope issue 如何在JS中嵌套内部函数中访问外部函数变量 - How to access outer function variable in nested inner function in JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM