Reading some articles about the javascript execution context. I my view is the following now (correct me if I'm wrong).
The execution context is the environment in which JS code is executed. There are three types of execution context:
- Global execution context: Default execution context in where the JS code starts its execution.
- Functional execution context: Context created of by the execution of code inside a function. If a function is called a context is put on the global execution stack.
- Eval:Inside an
eval()
function.
Execution context is a stack where items can be pushed on the stack (if new function are called) and popped of the stack (when functions are returned).
The JS engine creates the execution context in two phases:
- Creation phase
- Execution phase
In creation phase the JS engine has called a function but its execution has not started. During creation 3 events take place:
- Creation of the activation object: The activation object is a special object which contains all the variables, function arguments and inner functions declarations information.
- Creation of the scope chain: List of all the variable objects in which the current function exists.
- Value of this is determined
I suppose this is how closures are formed in Javascript under the hood.
Can anyone clearly explain what is the difference between an activation object
and a variable object
and how this relates to closures?