Understand js collection mechanism easy to understand version

  • 2021-01-11 01:53:00
  • OfStack

In the previous article, I explained the recycling mechanism in js, but at that time, I was a little confused about the concept of the recycling mechanism. Now I have a deeper understanding of the recycling mechanism, so I hereby publish this article to summarize it, so as to deepen my memory.
Why is there a recycling system? why?

For example, I have a memory card, the memory is 8 G, I put the files, video, music, are stored in the memory card, with my store content more and more, has not been able to save the memory card, if I want to put the other files saved to the memory card will need to delete some files, 1 but these deleted files is our own right, removed manually removed manually is equivalent to the delete js.

The same problem can occur in these programming languages, and yes, memory! Any variable we declare consumes memory, and the more variables there are, the slower it will run. Not just variables, of course, anything in the code. In order to solve these problems, the designers of these languages designed a set of code collection rules.

Code recycling rules are as follows:

1. Global variables are not recycled.

2. Local variables are recycled, which means that everything inside the function is destroyed after function 1 is run.

3. As long as it is referenced by another scope, it will not be collected

Let me prove this with a couple of examples.


function a(){
 var user = " Pursue the son ";
 return user;
}
var b = a();
console.log(b); // Pursue the son  

I am not supposed to be able to access variables in the function a, but I received the value of the function a after return through the global variable b, so the last piece of code looks like this.


function a(){
 var user = " Pursue the son ";
 return user;
}
var b = " Pursue the son ";
console.log(b);

As if this doesn't seem like code recycling, let's look at the next one.


function a(){
 var num = 0;
 return function(){
  num ++;
  console.log(num);
 };
}
var b = a();
b(); //1
b(); //2
b(); //3

See, if, in accordance with normal practice, the output should be the one to three times, because the body of the function 1 denier run this function code in the body will be cleared, since will be empty so next time you run this code num should be still 1, but there is a little different, as long as I said in the function above a local variable was quoted by another 1 scope then this code will not be destroyed.

The code above looks like this


function a(){
 var num = 0;
 return function(){
  num ++;
  console.log(num);
 };
}
var b = function(){
  num ++;
  console.log(num);
 };
b();
b();
b();

The scope of the anonymous function returned by the function a is changed from a to window. Because the anonymous function is referenced by the global variable b, it will not be destroyed.


function a(){
 var num = 0;
 return function(){
  num ++;
  console.log(num);
 };
}
var b = {
 fn:a()
}
b.fn(); //1
b.fn(); //2
b.fn(); //3

Also, because the anonymous function is referenced by the property fn of the object b, it changes its scope. In short, as long as a function or local variable is scoped, the function or local variable will not be destroyed.
That's all for this article, I hope you can help with the js collection mechanism.


Related articles: