1. What is a closure?
The "official" explanation is: the so-called "closure" refers to an expression (usually a function) with many variables and an environment bound to these variables ), therefore, these variables are part of the expression.
I believe that few people can directly understand this sentence, because he described it too academic. I want to use how to create a closure in JavaScript to tell you what a closure is, because it is very difficult to understand the definition of a closure directly by skipping the process of creating a closure. See the following section.Code:
Function (){
VaR I = 0;
Function B (){
Alert (++ I );
}
Return B;
}
VaR c = ();
C ();
This code has two features:
1. function B is nested in function;
2. function a Returns function B.
In this way, after var c = a () is executed, variable C actually points to function B and then executes C () then a window will pop up showing the I value (the first time is 1 ). This Code actually creates a closure. Why? Because variable C outside function a references function B in function A, that is:
When function a's internal function B is referenced by a variable outside function a, a closure is created.
I guess you still don't understand the closure, because you don't know what the closure will do. Let's continue exploring it.
Ii. What is the function of closure?
In short, the function of the closure is that after a executes and returns, the closure makes the garbage collection mechanism of JavaScript GC not to reclaim the resources occupied by, because the execution of the internal function B of A depends on the variables in. This is a straightforward description of the function of the closure, which is neither professional nor rigorous, but probably means that the process of understanding the closure needs to be gradual.
In the above example, because of the existence of the closure, the I in a always exists after function a returns, so that each execution of C (), I is the value of alert after auto-increment 1.
Then let's imagine another situation. If a does not return function B, the situation is completely different. Because after a is executed, B is not returned to the external world of A, but is referenced by A. At this time, a will only be referenced by B, therefore, functions A and B are referenced by each other without being disturbed by the outside world (referenced by the outside world), and functions A and B are recycled by GC. (The garbage collection mechanism of JavaScript will be described in detail later)
3. microview of the world in the closure
If you want to have a better understanding of closures and the relationship between function a and nested function B, we need to introduce several other concepts: the function execution environment (excution context), the activity object (call object) scope and scope chain ). The process from definition to execution of function a is used as an example to describe these concepts.
1. When defining function A, the JS interpreter sets the scope chain of function a to the "Environment" of function a when defining function ", if expression A is a global function, the scope chain contains only window objects.
2. When function a is executed, function a enters the corresponding execution environment (excution context ).
3. In the process of creating the execution environment, a scope attribute, that is, the scope of A, is added for A, and its value is the scope chain in step 1. That is, the scope chain of A. Scope =.
4. A call object is created in the execution environment ). An activity object is also an object with attributes, but it does not have a prototype and cannot be directly accessed through JavaScript code. After creating the activity object, add the activity object to the top of the scope chain of. In this case, the scope chain of A contains two objects: The activity object of A and the window object.
5. The next step is to add an arguments attribute to the activity object, which stores the parameters passed when calling function.
6. Add the parameters of all function a and the reference of function B to the activity object of function. In this step, the definition of function B is completed. As in step 1, the scope chain of function B is set to the environment defined by B, that is, the scope of function.
At this point, the steps from definition to execution of function a are completed. At this time, a returns the reference of function B to function C, and the scope chain of function B contains the reference to the activity object of function, that is to say, B can access all variables and functions defined in. Function B is referenced by Function C, and function B is dependent on function A. Therefore, function A is not recycled by GC after return.
When function B is executed, it will be the same as the above steps. Therefore, during execution, B's scope chain contains three objects: B's activity object, a's activity object, and window object, as shown in:
When function B accesses a variable, the search order is to first search for its own activity object. If it exists, it returns. If it does not exist, it will continue to search for the activity object of function, search by time until it is found. If the entire scope chain cannot be found, undefined is returned. If function B has a prototype object, search for its own prototype object after searching for its own activity object. This is the Variable Search Mechanism in JavaScript.
Iv. application scenarios of closures
1. Protect variable security in the function. Taking the initial example as an example, in function a, I can only access function B, but cannot access function B through other channels, thus protecting the security of I.
2. Maintain a variable in the memory. Still, for example, because of the closure, the I in function a is always in the memory, so each execution of C () will add 1 to the I self.
The above two points are the most basic application scenarios of closures. Many classic cases are based on this.
V. Garbage collection mechanism of JavaScript
In JavaScript, if an object is no longer referenced, the object will be recycled by GC. If two objects are referenced by each other and no longer referenced by 3rd, the two objects referenced by each other will be recycled. Because function a is referenced by function B and function B is referenced by Function C outside of function a, this is why function A is not recycled after execution.