In a closed article a few days ago we briefly introduced the closure, but there is no in-depth explanation, because the closure involves more knowledge points, in order to better understand the closure, today to explain about JS in the recycling mechanism.
In the first knowledge of the closure of the article I said JS in the recycling mechanism so that the matter, let us review, and in-depth understanding of JS in the recovery mechanism in the end is how the same thing.
function A () { var num = ten; returnfunction+ +; Console.log (num); }}a () (); 11a () (); One
Supposedly the second execution of the function A should print out 12, but the print is 11, this does not indicate that JS in this one did what hands and feet? Follow the normal logic and we will analyze the analysis together.
First take a look at our understanding :
We return an anonymous function in function A, we num++ in this anonymous function, and then we execute the anonymous function outside the function (the first parenthesis executes function a The second parenthesis executes the rutrun back function)
Now num is 11, and then we execute the function again, you should be 12, why not?
-------------------------------------the actual JS execution-----------------------------------
But JS designers in order to let the unnecessary variables in memory, (we write any variables are required memory space), what is called unnecessary variables? Which means that you don't need this variable to be destroyed? Then you will certainly ask JS how to know that those variables are what we do not need and what we need. So JS to know which variables need to be preserved, which do not need to be saved, will make some judgments. Next we will see how JS is judged.
1. The global variables defined in JS will not be destroyed, because we may use this variable at any time, so we cannot be destroyed.
2. However, the variables that are fixed in the function are not necessarily, and the reason that the function is destroyed by the life cycle of the variable that is defined by this function naturally does not save the last value.
2.1 But not that the function really does not save the last value, because sometimes we do need the last value, so JS to determine whether it is necessary to save the last variable value of the time will abide by such a rule.
Rules
If this function has been referenced by an external variable will not be destroyed (this sentence is not accurate enough, the following code will be explained step after year), otherwise destroyed. How do you understand this sentence?
function A () { var b = 0; return function (){ ++; Console.log (b); }} var d = A ();d();//1d ();//2
Function A is referenced by the variable variable d, more precisely the one in function A is referenced by the variable d, because the variable d equals the value of function a after execution, and since function a returns the anonymous function, it is accurate that the variable d equals the anonymous function. This anonymous function, because it uses the variable B in function A and is also referenced by the variable d, creates a closure that, as long as the variable d is not equal to NULL, then the variable B is saved to the variable D and will not be destroyed.
Summarize:
1, if an object is not referenced, then this object will be collected by GC;
2. If two objects refer to each other, but are not referenced by the 3rd object, then the two objects referenced by each other are also recycled.
If you want to learn more about closures, then I recommend that you take a look at the binding event in the For loop. The print variable i is the last issue in this article.
How to explain the recycling mechanism in JS again.