Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference and relationship between execution context and lexical environment?

Tags:

javascript

In JavaScript: Understanding the Weird Parts lexical environment is explained as the scope of your code while execution context is a collection of lexical environments, and that it includes stuff beyond your written code.

The descriptions of these terms still sound overlapping in functionality and it's unclear as to what execution context does or how it does it.

like image 493
Bar Akiva Avatar asked Mar 02 '16 22:03

Bar Akiva


People also ask

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 an execution context?

What is an Execution Context? Simply put, an execution context is an abstract concept of an environment where the Javascript code is evaluated and executed. Whenever any code is run in JavaScript, it's run inside an execution context.

What is the difference between scope and lexical environment?

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 does lexical context mean?

Lexical context analysis is the process of reasoning about the bindings in the context of a syntax template to predict the meanings of references in program fragments it produces. An important concern when using syntax templates is making sure their lexical context has the right bindings.


2 Answers

The best way to think of an execution context is as a stack frame, while lexical environments are indeed the scopes.

The respective spec chapters (§8.1 Lexical Environments and §8.3 Execution Contexts) explain:

  • Execution contexts contain the current evaluation state of code, a reference to the code (function) itself, and possibly references to the current lexical environments.
    Execution contexts are managed in a stack.
  • Lexical environments contain an environment record in which the variables are stored, and a reference to their parent environment (if any).
    Lexical environments build a tree structure.

With every change of the execution context, the lexical environment changes as well. However the lexical environment may change independently from that as well, for example when entering a block.

like image 188
Bergi Avatar answered Oct 01 '22 18:10

Bergi


Execution Context & Execution Context stack : Execution context is the internal javascript construct to track execution of a function or the global code. The js engine maintains a stack - execution context stack or call stack, which contains these contexts and the global execution context stays at the bottom of this stack. And a new execution context is created and pushed to the stack when execution of a function begins. A particular execution context tracks the pointer where statement of the corresponding function is being executed. An execution context is popped from the stack when corresponding function's execution is finished.

Lexical Environment : it's the internal js engine construct that holds identifier-variable mapping. (here identifier refers to the name of variables/functions, and variable is the reference to actual object [including function type object] or primitive value). A lexical environment also holds a reference to a parent lexical environment.

Now, for every execution context -- 1) a corresponding lexical environment is created and 2) if any function is created in that execution context, reference to that lexical environment is stored at the internal property ( [[Environment]] ) of that function. So, every function tracks the lexical environment related to the execution context it was created in.

And every lexical environment tracks its parent lexical environment (that of parent execution context). As a result, every function has a chain of lexical environments attached to it. [Note: in js a function is an object, creating a function by a statement means creating an object of type Function. So like other objects, a function can hold properties both internal and user defined]

The js engine search any variable or function identifier in the current lexical environment, if not found it searches the chain attached to the enclosing function. (for global code this chain does not exist). So, you understand how scoping of variables and functions are maintained. The concept of closure is also backed by, this chain (not a standard term, I just used it to ease the understanding). When a function instance is passed to another function as an argument, to be used as callback, it carries it's chain with it (sort of).

Note: the answer is based on what I learned from 'Secrets of the Javascript Ninja, 2/e'

like image 40
adnan2nd Avatar answered Oct 01 '22 17:10

adnan2nd