Consider the following JavaScript snippet:
function outer() { var x = 10; function inner() { var y = 20; } }
Obviously, the variable y not available in the outer context. Following the process in the ES5 specification, we find out that this happens when we enter the outer execution context:
We introduce a new execution context ( 10.4 )
Since this is a function, we perform the steps described in Entering the function code (10.4.3)
- Let the code be the value of the internal property Fs [[Code]].
The internal function property of the [[Code]] function is described in Creating function objects (13.2) . It refers to FunctionBody, as indicated in the grammar (all contents of the function):
function Identifier ( FormalParameterList opt ) { FunctionBody }
At this point, we enter the section "Binding mandatory messages (10.5) and do the following:
Create bindings for any arguments in the list of formal parameters of the function
For each function declaration in code:
Create a binding in the current scope between the function identifier and the function object (created as indicated in Function Definition (13) )
Nothing to say "remove the function we just processed from the code"
Create a binding for the arguments object
For each variable declaration in the code:
- Create a binding in the current area for the variable identifier
My question is why, at the moment, bindings are not created for declaring variables from internal functions? It seems that the code should still contain all the source code of the external function (which will include the source code of the internal function). I am looking for something in a specification that explains the behavior.
Updated to be a little understandable . I ask what happens when we enter the context of the outer function. The inner function is never called, and I don't care what happens when we return from the outer function. I'm just interested in the process defined by the specification of creating bindings for variable declarations when entering a new execution context.
source share