In the previous article I introduced "talk about my understanding of JavaScript prototypes and closures (handy notes 6)", and talk about my understanding of JavaScript prototypes and closures (handy notes 9) for details.
Execution context Stack
When the global code is executed, an execution context environment is generated, and each invocation of the function produces an execution context. When the function call completes, the context and the data in it are eliminated and then returned to the global context environment. There is only one execution context environment in the active state.
Stack out stack process----execution context stack:
var a = ten, //1. Into the global context environment
FN,
bar = function (x) {
var b = 5;
FN (x + b); 3. Into the FN function context
};
fn = function (y) {
var c = 5;
Console.log (y + c);
};
Bar (10);
1. Before executing, create the global context environment first
2, code execution, before execution to bar (10), variables in the global context are assigned during execution.
3, execute to bar (10) Call bar function, and create the execution context environment inside the function
4, this time the execution context of the pressure stack, set to active state
5. The bar function executes to FN (x + b), invokes the FN function, creates the execution context of the FN function, and presses the stack, which is set to the active state
6, the FN function completes, the FN environment out stack, is destroyed, releases the memory, the bar function becomes the active state
7. The FN function is executed and destroyed, which means that bar is executed, at this time the bar function environment is out of stack and destroyed. The global environment changes to an active state.
Here's how the code is executed.
According to the original author's description:
The variable assignment procedure for the context environment I omitted a lot, because those are not difficult, a look at the know.
Do! After the general execution of the code, you will know exactly what state the variable in each execution environment is.
Speaking of this, I have to regret to say to you: in fact, we have demonstrated that the above is a relatively ideal situation. There is a situation, and it is often used in a situation, can not be done so cleanly said destruction destroyed. This situation is great-closures. To say closure, we have to start with the free variable and scope.
The above is a small series to share my understanding of JavaScript prototypes and closures (handy note 8), I hope you like.