Why variables of internal functions do not flow into the external region?

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.

+4
source share
2 answers

You have a mistake in your thinking.

Javascript has a scope function. So you have a new execution context for each function, thats true. However, after returning from the function, this function context expires. It is for this reason that you cannot access variables from an internal function after it is returned. You are still in the context of the execution of an external function, but you can no longer access the context of the execution of an internal function.

Referring to spec :

Each return goes out of context. An exception thrown can also go out of one or more execution contexts.

EDIT: To clarify this further: The body of the function declaration is NOT processed (see 10.5.5.d ), only the Function ID and arguments are passed to the variable environment.

+1
source

It is directly in the definition of the term code (ยง10.1) :

A function code is the source code that is parsed as part of a FunctionBody . The functional code of a particular FunctionBody does not include any source code that is parsed as part of a nested FunctionBody .

0
source

Source: https://habr.com/ru/post/1436564/


All Articles