var foo = "Hello";
var c =(function a() {
function b(){
var bar = "World";
alert(foo bar);
return bar;
}
return b;
})()();
alert(foo c);
This example pops up hello world twice;
1. What is closure?
The "official" explanation is: the so-called "closure" refers to an expression (usually a function) that has many variables and an environment bound to these variables, so these variables are also part of the expression .
I believe that few people can directly understand this sentence because his description is 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 skip the creation process of a closure and directly understand the definition of a closure. Look at the following code:
function a(){
var i=0;
function b(){
alert( i);
}
return b;
}
var c = a();
c();
This code has two characteristics:
1. Function b is nested inside function a;
2. Function a returns function b.
In this way, after executing var c=a(), variable c actually points to function b. After executing c() again, a window will pop up to display the value of i (the first time is 1). This code actually creates a closure. Why? Because variable c outside function a refers to function b within function a, that is:
When the internal function b of function a is referenced by a variable outside function a, a closure is created.
I guess you still don’t understand closures because you don’t know what closures do. Let’s continue exploring.
2. What is the function of closure?
In short, the function of the closure is that after a is executed and returned, the closure prevents Javascript's garbage collection mechanism GC from reclaiming the resources occupied by a, because the execution of a's internal function b needs to depend on a. variables in . This is a very straightforward description of the role of closures. It is not professional or rigorous, but the general meaning is this. Understanding closures requires a step-by-step process.
In the above example, due to the existence of closure, i in a will always exist after function a returns. In this way, every time c() is executed, i will be the value of i that is alerted after adding 1.
Then let’s imagine another situation. If a returns something other than function b, the situation is completely different. Because after a is executed, b is not returned to the outside world of a, but is only referenced by a. At this time, a will only be referenced by b. Therefore, functions a and b refer to each other but are not disturbed by the outside world (referenced by the outside world). , functions a and b will be recycled by GC. (The garbage collection mechanism of Javascript will be introduced in detail later)
3. The microscopic world within the closure
If we want to have a deeper understanding of closures and the relationship between function a and nested function b, we need to introduce several other concepts: function execution context (execution context), active object (call object), scope (scope) ), scope chain. Take the process of function a from definition to execution as an example to illustrate these concepts.
1. When defining function a, the js interpreter will set the scope chain of function a to the "environment" where a is when defining a. If a is a global function, the scope chain will There is only window object.
2. When function a is executed, a will enter the corresponding execution context.
3. In the process of creating the execution environment, a scope attribute will first be added to a, which is the scope of a, and its value is the scope chain in step 1. That is, the scope chain of a.scope=a.
4. Then the execution environment will create an active object (call object). An active object is also an object that has properties, but it does not have a prototype and cannot be accessed directly from JavaScript code. After creating the active object, add the active object to the top of a's scope chain. At this time, a's scope chain contains two objects: a's active object and the window object.
5. The next step is to add an arguments attribute to the active object, which stores the parameters passed when calling function a.
6. Finally, add all formal parameters of function a and references to internal function b to the active object of a. In this step, the definition of function b is completed, so just like step 3, the scope chain of function b is set to the environment where b is defined, that is, the scope of a.
At this point, the steps from definition to execution of the entire function a are completed. At this time, a returns the reference of function b to c, and the scope chain of function b contains a reference to the active object of function a, which means that b can access all variables and functions defined in a. Function b is referenced by c, and function b depends on function a, so function a will not be recycled by GC after returning.
When function b is executed, it will be the same as the above steps. Therefore, the scope chain of b during execution contains 3 objects: b’s active object, a’s active object and window object, as shown in the following figure:
As shown in the figure, when accessing a variable in function b, the search order is to first search for its own active object, and return if it exists. If it does not exist, it will continue to search for the active object of function a, and search in sequence until Until found. If it cannot be found in the entire scope chain, undefined is returned. If function b has a prototype prototype object, it will first search for its own prototype object after searching its own active object, and then continue to search. This is the variable lookup mechanism in Javascript.
4. Application scenarios of closure
1. Protect the variables within the function. Taking the initial example as an example, i in function a can only be accessed by function b and cannot be accessed through other means, thus protecting the security of i.
2. Maintain a variable in memory. Still as in the previous example, due to closure, i in function a always exists in memory, so every time c() is executed, i will be incremented by 1.
The above two points are the most basic application scenarios of closures, and many classic cases originate from them.
5. Javascript’s garbage collection mechanism
In Javascript, if an object is no longer referenced, then the object will be recycled by GC. If two objects refer to each other and are no longer referenced by a third party, then the two objects that refer to each other will also be recycled. Because function a is referenced by b, and b is referenced by c outside a, this is why function a will not be recycled after execution.