Closures:A: To understand closures , you must first understand the special variable scope of JavaScript.
The scope of a variable is nothing more than two kinds: global variables and local variables.
The special point of the JavaScript language is that the global variables can be read directly inside the function.
varn=999; functionF1 () {alert (n); } f1 (); //999//on the other hand, a local variable inside a function cannot be read naturally outside the function. functionF1 () {varn=999; } alert (n); //Error/*Here's a place to be aware that when declaring variables inside a function, be sure to use the var command. If not, you're actually declaring a global variable! */ functionF1 () {n=999; } f1 (); alert (n); //999
B: How to read the local variables externally
For a variety of reasons, we sometimes need to get local variables within the function. However, as already mentioned, under normal circumstances, this can not be done, only through the workaround to achieve.
That is, in the inside of the function, define a function.
function F1 () {n=999; function// 999 }}
In the above code, the function F2 is included inside the function F1, and all local variables inside the F1 are visible to the F2. But the opposite is not possible, F2 internal variables, the F1 is not visible. This is the "chain-scoped" structure (chain scope) that is unique to the JavaScript language.
The child object looks up the variables for all the parent objects first-level. Therefore, all the variables of the parent object are visible to the child object, and vice versa.
Since F2 can read the local variables in the F1, we can not read its internal variables outside the F1 as long as the F2 is the return value!
function F1 () {n=999; function f2 () {alert (n); } return F2; } var result=// 999
What's the use of closures? 1: Take the variable inside the function 2: Let the values of these variables always remain in memory
function F1 () { var n=999; Nadd=function() {n+=1} function f2 () {alert (n); } return F2; } var result=// 999//
In this code, result is actually the closure F2 function. It runs altogether two times, the first value is 999, the second value is 1000. This proves that the local variable n in the function F1 is kept in memory and is not automatically cleared after the F1 call.
Why is that? The reason is that F1 is the parent function of F2, and F2 is assigned to a global variable, which causes F2 to always be in memory, and F2 's presence depends on F1, so F1 is always in memory and will not be reclaimed by the garbage collection mechanism (garbage collection) after the call ends.
Another notable part of this code is the line "nadd=function () {n+=1}", which first did not use the var keyword in front of Nadd, so Nadd is a global variable, not a local variable. Second, the value of Nadd is an anonymous function (anonymous functions), and this
The anonymous function itself is also a closure, so nadd is equivalent to a setter that can manipulate local variables inside the function outside of the function.