Skip to content Skip to sidebar Skip to footer

Parent Function Variable Access From Callback And Inline Self Invoking Function

I believed at first look both self invoking function is same only difference between them is in first one I am passing callback function and then executing by arguments object whil

Solution 1:

I am not able to figure out how It's working!!

JavaScript has lexical scope. That means the scope is determined by where the functions are defined inside the source code (unlike dynamic scope, where the scope is determined when the functions are called at runtime).

Lets add some names to your functions so we can reference them more easily:

(functionfoo(){
    var name = "Nishant";
    arguments[0]();
})(functionbar(){console.log(name);});

In this case bar is defined outside of foo and hence cannot have access to the variable defined inside of foo. In fact, at the moment bar is created, the variable name inside foo doesn't even exist yet, since foo hasn't been executed yet. Maybe it's easier to see when you do not inline the definition:

functionbar(){console.log(name);}

(functionfoo(){
    var name = "Nishant";
    arguments[0]();
})(bar);

This might look more familiar, and I bet you wouldn't expect that name inside bar has anything to do with name inside foo, right?


In the other case,

(functionfoo(){
  var name = "Nishant";
  (functionbar(){console.log(name);})()
})();

you defined barinside of foo. name is also inside foo and hence bar has access to that variable (lexical scope + closure).

Solution 2:

the "name" in the first version refers the global window.name property*, and in the second example to the private var name through the closure. The window.name property is optional and defaults to an empty string.

If this property wouldn't happen to exist your first example would throw a reference error.

The closure is created at the scope a function was declared, not at the point it got called and in the first example the function was declared inside the global scope, in the second inside the wrapper function scope.

window.name = "NoClosure";
(function(){
    var name = "Closure";
    arguments[0](); 
})(function(){console.log(name);}); //>"NoClosure"window.name = "NoClosure";
(function(){
    var name = "Closure";
    (function(){console.log(name);})(); //>"Closure"
})();

If you inspect the of the callback with console.dir(), you can see that it has no closure, here a snippet if you want to test it yourself.

(functionwrapper(){
    var isPrivate = "A closure could see me";
    console.dir(arguments[0]);
})(functioncallback(){});

Here is another experimental setup with 2 closures showing that function declared inside a call to a function are capable of getting a closure, but it will always be the closure of the scope of the function passing it, it can not access the scope of the function it was passed to as an argument and that later calls it as a callback.

(functionouterScope(){
   var outerVar = "in outer scope";
   (functioninnerScope(cb){
       var innerVar = "in inner scope";
       cb();
   })(functioncallback(){
       console.log(
          "outer:"+ typeof outerVar,
          "inner:"+ typeof innerVar);
       console.dir(arguments.callee);
   })
})()

*source https://developer.mozilla.org/en-US/docs/Web/API/Window.name

Post a Comment for "Parent Function Variable Access From Callback And Inline Self Invoking Function"