0

Here is the example I want to be enlighten on (Something that doesn't actually works).

 var myVar = 2;

    function a(){
     var myVar = 2;
        function b(){
            console.log(myVar);
        }
    };
   a();
   b();

This will output in the console: Uncaught ReferenceError: b is not defined.

As you can see I have a function named a with a nested function called b.

At first I thought I could invoke b outside a and get it working normally. I thought that would work because at first I call the a function.

By doing that I had in mind the fact that the a function would be put on the execution stack and during its creation phase the b function defined inside would be set in the memory.

As this is in memory I thought I could then execute this outside the function. Which obviously doesn't work.

So my conclusion is the b function is indeed set into memory during the creation phase of the a function but once the a function has finished to execute, once it's popped of the execution stack, the b function gets popped off the memory at the same time.

Thus calling it (I mean the b function) within the global scope is impossible.

Am I right on this ?

1
  • JavaScript has lexical scope and that applies to functions as well. Commented Nov 25, 2015 at 20:04

2 Answers 2

3

You are complicating things unnecessarily by speaking about execution stacks, creation phases and such.

The explanation is very simple: you cannot call b because the spec says that b is out of scope at the site where you are trying to call it. That is all, end of story.

Your example would actually work if converted to PHP, which makes me think that perhaps this is where you got the idea from. But JS and PHP are different languages and the (IMO ridiculous) manner that PHP treats nested functions does not transfer over.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep, you are right, I try to go too deeply in the reflexion... I knew I can't do that in Javascript, but I was trying to fully understand why. All the mechanism involded. But in fact It's even more simpler. The explaination is the function named 'b' sits inside the function named 'a'. Then It fails when we try to call it from the global scope, because the global execution environment will look for the function b, but that was never added to its variable environment during the creation phase of the global execution context... In summarize, they are not in the same scope as you told me. Thx
0

If you want to call b outside of a, you need to create a reference to it outside of a:

var myVar = 2;

function a(){
  var myVar = 2;
  function b(){
      console.log(myVar);
  }
  return b;
};

var b = a();
b();

This however won't cause b to print the global myVar. It will still have access to the myVar inside the closure scope of a.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.