Closure is an important feature of ECMAScript, but it is difficult to describe it with a proper definition. Although the closure is difficult to clearly describe, it is easy to create, or accidentally create. However, the existence of closures actually has some potential problems. To avoid creating a closure accidentally and make better use of the advantages of the closure, it is necessary to understand the closure mechanism. Closure is a very important feature of ECMAScript, however, it is difficult to describe it with a proper definition. Although the closure is difficult to clearly describe, it is easy to create, or accidentally create. However, the existence of closures actually has some potential problems. It is necessary to understand the closure mechanism to avoid "accidentally" Creating a closure and to better utilize the advantages of the closure.
Closure Definition
There are too many definitions about closures, especially some definitions that are very abstract, like this:
A "closure" is an expression (typically a function) that can have free variables together with an environment that binds those variables.
Generally, a closure is an expression with some free variables and an execution environment bound to these variables. This definition is too concise, but hard to understand.
There is another definition:
All functions are closures. This definition gives me a lot of confusion. In other words, because Javascript does not have block-level scopes, closures generally refer to functions (I don't know which methods can constitute closures besides functions ).
I don't want to talk too much about the relationship between functions and closures here. The following is a definition that I think is easier to understand.
First, the closure is based on the scope chain. Due to the mechanism of the scope chain, all functions (even global functions) can reference variables (free variables) in the context execution environment ).
Second, the closure must have free variables. By the way, two variables: 1. Local variables (bound variables) 2. Non-local variables (free variables)
Finally, it still exists after the Context Environment ends. That is, internal functions have a longer life cycle than their external functions.
Analysis of closure Definitions
The two points of closure definition have always been considered whether they must be satisfied at the same time.
First, if the closure does not have free variables, that is, it does not access external variables, it will lose the meaning of the closure. (Unless the behavior is changed through other closures) Therefore, I think free variables is a necessary condition.
Second, if the function has free variables, but the context environment is destroyed, it will also be destroyed. As you can imagine, although an internal function accesses its external function variables, it is recycled after the external function is executed. In this case, the closure discussion is meaningless.
Let's look at two examples:
The Code is as follows:
Var objectA = (function (){
Var localA = "localA ";
InnerFn ();
// Simple internal function call
Function innerFn (){
LocalA = "innerChange ";
}
Return {
GetLocalA: function (){
Return "empty ";
}
};
})();
ObjectA. getLocalA ();
ObjectA. getLocalA = function (){
Return localA;
};
// Console. log (objectA. getLocalA (); // error: localA is not defined
Var objectB = (function (){
Var localB = "localB ";
Return {
GetLocalB: function (){
Return "empty ";
},
UpdateGetLocalB: function (){
This. getLocalB = function (){
Return localB;
};
},
UpdateLocalB: function (){
LocalB = "changeLocalB ";
}
};
})();
Console. log (objectB. getLocalB (); // empty
// Change through other closures
ObjectB. updateGetLocalB ();
Console. log (objectB. getLocalB (); // localB
ObjectB. updateLocalB ();
Console. log (objectB. getLocalB (); // changeLocalB
Advantages and disadvantages of closures
The advantage of closures is that they can access the parameters and variables (except this and arguments) that define their external functions ).
The main problem with closures is that they store the scopes of the functions that contain them. Therefore, they occupy more memory space than general functions, so they should not be excessively used.
Closure Application
The most basic application scenario of closures is to implement private by protecting internal variables, such as the module mode.