The knowledge of the context and scope mentioned earlier, in addition to understanding this knowledge, is the basis for understanding closures.
As for the concept of "closure" the word description, really bad explanation, I have seen many times, but now still can't remember.
But you just need to know two things about the application--the function as the return value, and the function as a parameter pass.
First, the function as the return value
As the above code, the bar function is assigned to the F1 variable as the return value. When executing F1 (15), the value of the max variable under the FN scope is used. As for how to take values across scopes, you can refer to the previous section.
Second, the function is passed as a parameter
As in the code above, the FN function is passed as one parameter into another function, assigned to the F parameter. When you execute F (15), the value of the max variable is 10, not 100.
In the previous section, when the free variable was evaluated across scope, it was emphasized that the scope of the function should be created instead of the parent scope. Understand this, in the above two-way code, how the free variable value should be relatively simple. (Do not understand the friend must go to the last section to see, this is very important!) )
In addition, when it comes to closures, you need to combine the execution context stack with a combination of scopes.
When executing the context stack (http://www.cnblogs.com/wangfupeng1988/p/3989357.html), we mentioned that when a function is called, its execution context is destroyed and the variables are destroyed at the same time.
But there was a question mark in that article at the time-in some cases, after the function call was completed, its execution context was not destroyed. This is what is needed to understand the core of closures.
We can take the first paragraph of this article (slightly modified) to analyze the code.
The first step is to generate the global context environment before the code executes and assign values to the variables at execution time. The global context environment is active at this time.
The second step, when executing the 17th line of code, calls FN (), produces the FN () execution context, presses the stack, and sets the active state.
The third step, after executing line 17th, the FN () call is complete. It is supposed to destroy the execution context of FN (), but this cannot be done here. Note that the emphasis is on: Because a function is returned when the FN () is executed. The special thing about functions is that you can create a separate scope. And coincidentally, in the body of the returned function, there is also a free variable Max to refer to in the FN () context under the FN scope. Therefore, this max cannot be destroyed, and Max in the bar function will not be able to find the value after destroying it.
Therefore, the FN () context here cannot be destroyed and still exists in the execution context stack.
-that is, when the 18th row is executed, the global context becomes active, but the FN () context is still in the execution context stack. In addition, when you finish line 18th, Max in the global context environment is assigned a value of 100. Such as:
Fourth, executes to line 20th, executes F1 (15), that is, executes bar (15), creates a bar (15) context, and sets it to the active state.
When you execute bar (15), Max is a free variable and needs to be found in the scope where the bar function was created, and the value of Max is 10. This process has been mentioned in the scope chain section.
The point here is that creating the bar function is created when you execute FN (). FN () has long been executed, but the FN () execution context still exists with the stack, so when bar (15), Max can find it. If the FN () context is destroyed, then Max cannot find it.
Using closures increases the cost of content, and now it's obvious!
The fifth step, the execution of 20 lines is the context of the destruction process, here will not repeat.
Closures and scope, contextual environment has an inseparable relationship, really is "want to say love you not easy"!
In addition, closures are very much used in jquery, and there are a few examples here. So, whether you want to know a classic framework/class library, or want to develop a plug-in or class library, like closures, prototypes of these basic theories, it must be known. Otherwise, you do not know why, because the bugs may be completely outside your knowledge.
By now the closure of the simple introduction, the next section we summarize.
---------------------------------------------------------------------------
In-depth understanding of JavaScript prototypes and Closures (15)--closures