Search code examples
javascriptscopeexecutioncontext

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

Solution

  • Because there is actually no scoping here. All of this accesses refer to the window object. Hence, when you are editing this.myRefVar at the inner scope, you are actually editting the value at the 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.
    

    What's going on in your code is actually not scopping but closure. For better understanding:
    Scopping
    Closures
    Similar SO question