0

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:

  1. Global execution context: Default execution context in where the JS code starts its execution.
  2. 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.
  3. 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:

  1. Creation phase
  2. Execution phase

In creation phase the JS engine has called a function but its execution has not started. During creation 3 events take place:

  1. Creation of the activation object: The activation object is a special object which contains all the variables, function arguments and inner functions declarations information.
  2. Creation of the scope chain: List of all the variable objects in which the current function exists.
  3. 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?

Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155

1 Answers1

0

Variable object (VO) is the array of all the parent scopes . So whenever a variable value is looked for. it is first looked in Active Object . if the value is not available in AO . VO is looked for the first VO element which has the definition. You can imagine VO as

VO = [AO1,AO2, AO3, AO4]
current Active object is AO
All the AO are objects here 
lets say you look for the value of variable.

VO is iterated and if any varibale found with the same name, value is returned.

Note: I dont know the internals of vo and Ao much. I am open to correct myself if i am wrong

simbathesailor
  • 3,681
  • 2
  • 19
  • 30