Today, I want to make a deep understanding of JavaScript's execution context. Have always had this plan, but helpless busy doings wherever forget. Read a blog today, the implementation of the context of the understanding can be said to be clairvoyant.
First, the understanding of a piece of code begins
Enter the following code snippet in the browser's console:
This result shows that the code before the execution of a sentence, the browser has done some preparatory work, so, the following two are not error. Let's look at a piece of code below:
The result shows that when the function is declared, the entire function is assigned a value, and the function expression and the variable are just declarations.
Let's summarize what has been done in the prep work:
- Variable, function expression--variable declaration, the default assignment is undefined;
- this--assigned value;
- function declaration--Assignment value;
The readiness of these three kinds of data we call "execution context" or "execution context".
Second, the above is under the global scope of the execution context, the following look at the scope of the function under the context of the environment
The code above shows that the parameters of the arguments variables and functions have been assigned before the statement execution of the function body. As you can see from here, each time a function is called, a new execution context is generated.
Look at the code again:
As you can see, when the function is defined (not when it is called), the scope of the free variable inside the function body is determined.
The final summary:
The context data contents of the global code are:
Common variables (including function expressions), such as: var a = 10; |
Declaration (default assignment is undefined) |
function declaration, such as: function fn () {} |
Assign value |
This |
Assign value |
If the code snippet is a function body, you need to attach it on this basis:
Parameters |
Assign value |
Arguments |
Assign value |
Value scope of free variables |
Assign value |
Give the execution context a common definition-- before executing the code, all the variables that will be used are taken out beforehand, some are directly assigned, and some are first empty with undefined.
JavaScript Execution Context Environment