The premise of the closure is that the personal understanding is to read the local variables from the outside, under normal circumstances, this is impossible. A simple closure example is as follows:
1 functionF1 () {2 3n=100;4 5 functionF2 () {6 alert (n);7 }8 9 returnF2;Ten One } A - varresult=F1 (); - theResult ();// -
The F2 function in the code is the closure.
1 functionF1 () {2 3 varn=100;4 5Nadd=function() {N+=1}6 7 functionF2 () {8 alert (n);9 }Ten One returnF2; A - } - the varresult=F1 (); - -Result ();// - - + Nadd (); - +Result ();//101
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.
Take a look at the scope chain:
When code executes in an environment, a scope chain of variable objects is created to guarantee orderly access to variables and functions that the execution environment has access to.
function A (x, y ) {var b=x+y; return b; }
When function A is created, its scope chain fills in global objects, and global objects have all global variables:
If the execution environment is a function, then its active object (Activation object, AO) is the first object of the scope chain, the second object is the containing environment, and the next is the containing environment that contains the environment:
function A (x, y ) {var b=x+y; return b; } var tatal=a (5,10);
At this time var total=a (5,10); The scope chain of the statement is as follows:
The parsing of identifiers during function runs is the process of searching at the first level of the scope chain, starting with the initial object, and backtracking backwards until the identifier of the same name is found, and no further traversal is found, and an error is not found.
the relationship between the scope chain and the closure: each function has its own execution environment, when the execution flow enters a function, the environment of the function is pushed into a function stack, and after the function is executed, the environment is executed and destroyed, and all the variables and function definitions stored therein are destroyed. Control is returned to the previous execution environment, and JavaScript needs to preserve the referenced function as long as there is the possibility of invoking an intrinsic function. and the JavaScript runtime needs to keep track of all the variables that reference this intrinsic function until the last variable is discarded, and the JavaScript garbage collector frees up the appropriate memory space . Look back. A lot of understanding, the parent function defines the variable in the scope chain of the child function (the parent function's variable is visible in the child function), the child function is not destroyed, its scope chain all variables and functions will be maintained, will not be destroyed.
Personal understanding of JavaScript scope chains and closures