Basic concept
An execution context is an abstraction of the context in which JS code is parsed and executed
We can think of it as an object, and an execution context contains the following:
-
The variable object
Each execution environment has a variable object. The variable object of the global execution environment always exists, while the variable object of the local environment, such as the function, only exists during the execution of the function. The argument list in the function code block, declared variables and functions to be added as properties to the variable object, is not directly accessible.
-
Active objects
When the function is executed, the previously inaccessible variable object is activated as an active object, and from there, we can access its various properties.
-
The scope chain
A scope specifies how to find a variable, that is, determine what access the currently executing code has to the variable. The scope of a function is determined when the function is created. When a variable is searched, it is first searched from the variable object of the current context. If it is not found, it is searched from the variable object of the parent execution context until it finds the variable object of the global context, that is, the global object.
-
Caller information
If the current function is called as an object method or a delegate call is made using apis such as Bind Call Apply, the caller information of the current code block is put into the current execution context, otherwise it defaults to a global object call.
Execution context type
-
Global execution context: Code that is not in any function is in the global execution context. Only one global execution context can exist in a program. The global execution context is created and destroyed only when the browser window is opened and closed
-
Function execution context: Every time a function is called, a new function execution context is created, regardless of whether the function is called repeatedly
-
Eval execution context: Code running in Eval also gets its own execution context and is rarely used.
The life cycle
Create a stage
-
Create a variable object:
Variable promotion: In this stage, the parameter list is declared and assigned retrograde first, and variables and functions are initialized. The variable is uniformly defined as undefined, and the exact value will not be available until the assignment, while functions will be initialized directly. In this phase, when there are multiple variable declarations with the same name, the function overrides all other declarations, and if there are multiple function declarations with the same name, the last function declaration overrides all previous declarations.
function func(a) { console.log(a); var a = 10; Function a(){}} func(100) //function a(){} //function func(a) {console.log(a); var a = 10; var a = function () {} } func(100) //100Copy the code
-
Create scope chains
-
Make sure this points to
The value of this is checked at execution time, not at definition
Execution phase
In the execution phase, THE JS code starts to execute line by line. In this phase, the JS engine starts to assign values to defined variables, starts to access variables along the scope chain, and creates a new execution context if there is an internal function call and pushes it onto the execution stack
Destruction of phase
When the function completes execution, the current execution context (local environment) is popped from the execution context stack and destroyed. Closures for special circumstances, when the father of the closure function, after the completion of the parent function itself the scope chain execution environment will be destroyed, but due to the closure of the scope chain is still in the variable object of the parent function, led to the parent function variable object will have been resident in memory, cannot be destroyed, unless the closure of the reference is destroyed, closure variable object of the parent function no longer, This block of memory can be freed. Overuse of closures can cause memory leaks.