Order: Closure This thing AH ~ in a lot of languages without code blocks will appear, has become a gateway to the majority of programmers, closures so many programmers feel obscure (in fact, Baidu this noun, really is very obscure Ah Pro ==| | | ), the first time I know that the term closure is from the "JavaScript authoritative guide" read, then 2015 March, I just began to see the winter vacation purchase book, saying that then I did not break up with the first love, I clearly remember that I was in Xuzhou back to Suzhou on the train on the whole study of this " JavaScript authoritative guide of the Cock silk scene (long distance Love), the former girlfriend said the book on the cover of the Rhino ugly dead, I said, "You know what!" "==| | |
Words do not say, after some memories, I think of the closure of this thing until the 5 months after the breakup has not been clear, until one day I finished in the laboratory three JS code comparison, I realized this book of God's talk of the closure of the idea!
First look at the closure of the Baidu Encyclopedia, and look (with the picture):
Closures are blocks of code that can contain variables that are free (not bound to specific objects), that are not defined within this block of code or in any global context, but are defined in the context in which the code block is defined (local variables). The word "closure" derives from the combination of the following: the code block to be executed (since free variables are included in the code block, these free variables and the objects they refer to are not freed) and the computing Environment (scope) that provides the binding for free variables.
In Scala, Scheme, Common Lisp, Smalltalk, Groovy, JavaScript, Ruby, Python, Go, Lua, objective C, Swift and Java (JAVA8 and above) can find different levels of support for closures.
At first glance, Baidu said marvellous, what code block, what local variables, plainly, we just understand the JS code a little principle: scope of the release and the existence of problems.
Closures (closure) are a difficult and unique feature of the JavaScript language, and many advanced applications rely on closures.
Closures have three properties:
1. function Nesting functions
2. external parameters and variables can be referenced inside the function
3. parameters and variables are not recycled by the garbage collection mechanism
Compare the following three sections of code:
And look at the following code:
// application and change of global variables = 1 // global variable function change_global () { var global = 2; // redefine global as a local variable, and no var becomes a reference global variable alert (GLOBAL);} Change_global (); Alert (GLOBAL);
Browser display: First 2, after 1.
Conclusion: It is verified that the function changes the local variable, but does not change the global variable, but once we remove the Var from the local variable global, the result is 2, 2. After the function is executed, the scope chain of the function is destroyed, and the local variables are destroyed! Global variables to be destroyed when the page closes (program ends)
If you are familiar with the JS syntax and scope chain, you can skip the above code, I am to say the following code!
And look at the following code:
// z is a normal function that is destroyed once the scope chain is executed Span style= "COLOR: #0000ff" >var function Z (t) { var s=[1,2,3]; // local variable s S.push (t); // add a number to the S array return S;} Z ( 4); // Destroy this after execution z ( 5); // Destroy this after execution alert (z ( 6)); // destroy this after execution
Browser display: 1,2,3,6.
Conclusion: What about 4,5? Of course, because the function was destroyed after the last two executions, only 6 was added, which fully proves the principle of the JS execution function in memory-destroying everything in function after execution!
The above words are not entirely right, really is everything?
And look at the following code:
//x This is a closure function that will be destroyed at the end, but because he returned another function, the function called and returned the local variable s, so s cannot be destroyed and must be preserved! //x This is a closure function that will be destroyed at the end, but because he returned another function, the function called and returned the local variable s, so s cannot be destroyed and must be preserved! functionx () {vars=[1,2,3];//the same local variable s return function(t) {S.push (t); //add numbers to S as well. returns; };}varY=x ();//The closure is assigned to the Y variableY (4); Y (5); alert (Y (6));
Browser display: 1,2,3,4,5,6.
Conclusion: function X () is actually a closure of the code block environment, and the s variable in the closure is not destroyed because the x executes, nor is it destroyed after the function is executed, even if the code is completely executed, s remains in memory, and the scope chain still exists. Know the end of the program (close the browser)! So note: Abuse of closures can lead to low memory!
In order to better explain the problem of destruction and non-destruction, I recommend the following code
// Global object y, variable in: Never destroyed before program ends // A local object in a function is destroyed like a variable after the function ends. var y={ s:[), // the method in the variable // object in the object (similar to a function) Add:function(t) { this. S.push (t); return This . S; }} Y.add (4); Y.add (5); alert (Y.S) ;
Browser display: 1,2,3,4,5,6.
Conclusion: The same effect is achieved with the object and the closure, because the object is persisted in the current program memory unless it is specified to be destroyed!
The use of closures has an advantage, but also its disadvantage, is that local variables can be resident in memory, you can avoid the use of global variables. Global variables can be called in every module, which is bound to be catastrophic.
JavaScript authoritative guide Learning--js closures