JavaScript context should be the basis of the language of JS, if you understand it, you can clearly understand the JS statement execution process, better analysis of the code.
Context Classification :
1) Global-level context 2) function-level context 3) the context within which the Eval function runs
Execution context Stack
In JavaScript advanced programming, the context is called the execution environment, which actually expresses the same thing.
In the browser, the JavaScript engine works in a single-threaded way. That is, only one event at a time is activated and other events are placed in the queue waiting to be processed.
We already know that when JavaScript code files are loaded by the browser, the default first entry is a global execution context. When a function is called in the global context, the program flow enters the called function, and the engine creates a new execution context for the function, and presses it into the top of the execution context stack. The browser always executes the current context at the top of the stack, and once executed, the context is ejected from the top of the stack and then into the context under which the code executes. The context in the stack is then executed sequentially and the stack pops up until it returns to the global context.
Execution context Creation process
Consider the execution context as an object with the following 3 properties:
executioncontextobj = { /** / },/* */ }, this: {}}
Execution process:
1 Find the code for the calling function in the current context
2 before executing the code in the called function body, start creating the execution context
3 Establishment phase:
Creating Variableobject Objects
1. Establish the arguments object, examine the parameters in the current context, establish the properties and property values under the object
2. Check the function declaration in the current context:
Each time a function declaration is found, a property is established with the function name under Variableobject, and the property value is a reference to the address of the function in memory.
If the above function name already exists under Variableobject, then the corresponding property value will be overwritten by the new reference.
3. Check the variable declaration in the current context:
Every declaration of a variable is found, under Variableobject, a property is created with the variable name, and the property value is undefined.
If the variable name already exists in the Variableobject property, skip directly (prevent the value of the property pointing to the function from being overwritten by the variable property to undefined), the original property value will not be modified.
Initialize the scope chain
Determines the pointing object of this in context
4. Code Execution phase:
Executes the code in the function body, runs the code one line at a line, assigns a value to the variable attribute in the Variableobject.
Understand that the execution context should understand the scope elevation and variable declarations of function declarations in advance.
JavaScript execution context