Understanding the Javascript closure (closure)
Topic original post http://www.w3cgroup.com/article.asp? Id = 87
This article introducesJavascript Closure
.
After reading it, my simple understanding of the Javascript closure is the "maintaining a variable in the memory will not be recycled by GC" mentioned in sections 4 and 5 of the article ".
Of course, you still need to learn to understand JavaScript closures in depth.
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 creation process of the closure.
See the following 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 cited by B.
Therefore, if function a and function B reference each other but are not disturbed by the outside world (referenced by the outside world), function a and function B will be recycled by GC. (The garbage collection mechanism of JavaScript will be described in detail later)
3. microview of the world in the closure
To learn more about closures and the relationship between function a and nested function B, we need to introduce another concept: the execution environment of functions (excution
Context, activity object, scope, 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 will
Scope chain)
Set to define the "Environment" of a when a is set. If a is a global function, only the window object is in scope chain.
2. When function a is executed, function a enters the correspondingExecution Environment (excution context)
.
3. When creating the execution environment, a scope attribute is added for A, that isScope
The value is the scope chain in step 1. That is, the scope chain of A. Scope =.
4. Then, the execution environment will createActivity object)
. 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. At this time, the scope chain of A contains two objects:
Activity object and 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 Depends on function A. Therefore, function A is not returned.
GC collection
.
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 until it is found. If the entire scope chain cannot be found, undefined is returned. If function B has a prototype object
Then, search for its own prototype object after finding its own activity object, and then continue searching. 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.