Reference: http://www.cnblogs.com/wangfupeng1988/p/3989357.html
Continue with the above content.
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.
In fact, this is a stack of stacks of the process-execution context stack. The following figure:
According to the following code to detail the context stack of the stack, the stack process.
As on the code.
The global context environment is first created before the code is executed.
Then the code executes. Before the code executes to line 12th, variables in the context environment are assigned during execution.
Executes to line 13th, calling the bar function.
Jumps inside the bar function, and before executing the function body statement, a new execution context environment is created.
and the execution context of this implementation of the stack, set to active state.
Executes to line 5th, and the FN function is called. Into the FN function, the execution context of the FN function is created and the stack is set to the active state before the function body statement is executed.
Wait for line 5th to finish, that is, after the FN function completes, this call FN generated the context of the stack, and was destroyed (has been used, it is necessary to destroy in time, free memory).
Similarly, after the 13th row completes, that is, the bar function completes, the call bar function generated by the context of the stack, and was destroyed (has been used, it is necessary to destroy in time, free memory).
Well, I am very patient to introduce a short code of the execution context of the change process, a complete closed loop. The variable assignment procedure for the context environment I omitted a lot, because those are not difficult, a look at the know.
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.