One, what is closures
Distance from the last update for some time, you know the new year, then talk about the advanced use of JavaScript-closures. JS closure is not really difficult to understand the advanced concept, but some of the books are not easy to understand or cite examples are not very appropriate, now we have a simple example to understand the "closure." As a programmer, perhaps a piece of code, more descriptive than the text description of the problem, the code to speak, seeing is real.
Well, let's look at the following code:
function T1 () { var = +; Function T2 () { alert (age); } return t2; } var age = +; var temp = t1 (); Temp ();//20
In order to highlight the effect of execution, a age=100 variable is defined in the outer layer of the T1 function, and the var temp = T1 () is executed. , the function T2 assigned to temp, so that temp is actually a reference to the T2 function, followed by the execution of temp, the output is 20, no doubt, the variable age is under the scope of the T1 function, you have seen the JavaScript language and other languages different places.
In most languages, such as Java, the local variables within the function T1 are destroyed as the function T1 execution ends, but in JS, the variable age=20 is T2 caught, even after the T1 function is finished, it can still be accessed through T2 to the age
In this case, the returned T2 function is not isolated, even putting the surrounding variable environment, forming a ' closed package ', returning together, so called ' closure '
Second, the closure of the application
Since JavaScript has implementations of closures, how are javascript closures used or in what cases? As already seen in the above example, the function T1 inside the age variable can only be accessed through the returned T2 function, then age is not the equivalent of Java private variable it, haha, the next one will talk about, and will be "closure" to encapsulate the attributes inside, made interface, others can not see And will not contaminate other variables, think about it.
Speaking so much, let's take a practical example to see:
Requirements: Implement a counter, each call a function, so that the value of the timer plus 1
1, closed package implementation
function counter () { var i = 0;//But after execution of counter, except for the returned INC function, other functions cannot access the variable i var inc = function () { return ++i; } return Inc; var c = counter (); Alert (c ()); Alert (c ()); Alert (c ());
2, can be combined with "anonymous" function, to simplify
var counter = (function () { var i = 0; return function () { return ++i; } }) (); Alert (counter ()); Alert (counter ()); Alert (counter ());
I believe you have seen the benefits of closures, but please do not use closures, because in the Internet Explorer is a "count" of the way to track objects for garbage collection, because of the new closure, will not release local variables, if caused by excessive circular reference, will cause "memory leak."
4. JavaScript Advanced Closure Package