Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a function expression have its own scope/lexical environment

I'm reading the Execution Context / Lexical Environment section of the ECMA 262 5 specification. It states the following: (emphasis added)

A Lexical Environment is a specification type used to define the association of Identifiers to specific variables and functions based upon the lexical nesting structure of ECMAScript code. A Lexical Environment consists of an Environment Record and a possibly null reference to an outer Lexical Environment. Usually a Lexical Environment is associated with some specific syntactic structure of ECMAScript code such as a FunctionDeclaration, a WithStatement, or a Catch clause of a TryStatement and a new Lexical Environment is created each time such code is evaluated.

I noticed that it says nothing about creating a lexical environment for Function Expressions. Is a lexical environment created for function expressions, or is one created only for function declarations? Am I missing something?

Edit: I notice that function code will have its own execution context, which is why I'm also confused why function expression is not mentioned in the lexical environment section.

like image 242
contactmatt Avatar asked Feb 22 '13 22:02

contactmatt


People also ask

Is lexical environment the same as scope?

Lexical Environment is the environment of the function where it is written. That is, the static order/place where it is situated, regardless from where it is called from. Scope of a variable/function is basically the locations from where a variable is visible/accessible.

What is the lexical environment?

A lexical environment is a data structure that holds identifier-variable mapping. (here identifier refers to the name of variables/functions, and the variable is the reference to actual object [including function object or primitive value]. Lexical in general means in hierarchy or in a sequence.

What is the lexical scope?

Lexical scoping, also known as static scoping, is a convention used with many modern programming languages. It refers to setting the scope, or range of functionality, of a variable so that it may be called (referenced) from within the block of code in which it is defined.

What is lexical scope example?

Example of Lexical Scope Is it the global scope or the getName() function's local scope? Answer: Remember that lexical scope means definition space — not invocation space. Therefore, myName 's lexical scope is the global scope because we defined myName in the global environment.


2 Answers

Yes, every function gets (§10.4.3) its own ExecutionContext when it is called (§13.2.1). That new context is initialised with a new LexicalEnvironment (created by NewDeclarativeEnvironment, §10.2.2.2), deriving from the [[Scope]] of the function - i.e. the LexicalEnvironment it was declared/"expressed" in (§13).

As @Pointy pointed out, the sentence you stumpled upon does not exhaustively list them: "…some [structure] such as…".

like image 91
Bergi Avatar answered Oct 04 '22 02:10

Bergi


If a name is included in a FunctionExpression, that name becomes a read-only binding that shadows any outer declarations of the same name. But that binding may itself be shadowed by a formal parameter or local declaration within the function. Such a binding for the function name is only created for named FunctionExpressions and not for anonymous FunctionExpressions or FunctionDeclarations. The name binding for a FunctionDeclaration is created in the surrounding VariableEnvironment.

Here is a more detailed explantion referencing the ES5.1 spec:

There is more than one environment record associated with a function object. Whenever a function is called a new DeclarativeEnvironmentRecord is created to contain the local bindings of that particular function invocation. That record becomes both the VariableEnvironment and the initial LexicalEnvironment of the ExecutionContext that is created for that invocation. This is specified in section 10.4.3.

When this environment record is created its "outer environment" is set to the value of the [[Scope]] internal property of the function object that is being called. (line 5, 10.4.3) The outer environment provide the bindings for all non-local declarations. The [[Scope]] is set when a function object is created (see the semantics in section 13 and also 13.2). So, each distinct invocation of a specific function object has a different local environment but all invocations of that function share the same outer [[Scope]].

For most functions, the captured [[Scope]] is simply the LexicalEnvironment of the ExecutionContext that was active when the function is created. However FunctionExpressions that include an Identifier as the function name have an extra DeclarativeEnvironmentRecord insert at the head of its [[Scope]] chain. (see steps 1-3 of the third algorithm in section 13).

This extra environment record is used to capture the binding for the function name given in the FunctionExpression.

like image 28
Allen Wirfs-Brock Avatar answered Oct 04 '22 03:10

Allen Wirfs-Brock