Home > Web Front-end > JS Tutorial > body text

Understanding the js recycling mechanism in an easy-to-understand version_javascript skills

WBOY
Release: 2016-05-16 15:13:08
Original
997 people have browsed it

In the previous article, I explained the recycling mechanism in js, but for me at the time, I was a little confused about the concept of recycling mechanism. Now I have a deeper understanding of the recycling mechanism, so hereby Post this article to summarize and deepen your memory.
Why is there a recycling mechanism? why?

For example, I have a memory card. This memory is 8G. I save files, videos, and music to this memory card. As I store more and more content, this memory card has It cannot be saved. If I want to save other files to this memory card, I need to delete some files. But these deleted files are deleted manually by ourselves, right? Manual deletion is equivalent to delete in js.

These problems also occur in these programming languages, yes, memory! Any variables we declare consume memory, and the more variables we have, 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 recycling rules.

Code recycling rules are as follows:

1. Global variables will not be recycled.

2. Local variables will be recycled, that is, once the function finishes running, everything inside the function will be destroyed.

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

I use a few examples to prove these.

function a(){
 var user = "追梦子";
 return user;
}
var b = a();
console.log(b); //追梦子 
Copy after login

Logically speaking, I cannot access the variables in function a, but I received the return value of function a through global variable b, so the final code became as follows.

function a(){
 var user = "追梦子";
 return user;
}
var b = "追梦子";
console.log(b);
Copy after login

It seems that there is no code recycling in this, so let’s look at the next piece of code.

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

See, if you follow the normal practice, the output should be 3 times and 1 time, because once the function body is run, the code in the function body will be cleared. Since it will be cleared, run this section next time When writing the code, num should still be 1, but the situation here is a little different. I said above that as long as the local variables in the function are referenced by another scope, this code will not be destroyed.

The above code looks like this

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

Then the scope of the anonymous function returned by function a is shifted from function a to window. Since this anonymous function is referenced by 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
Copy after login

The same is true, because the anonymous function is referenced by the attribute fn of object b, changing its scope. Simply put, as long as a function or local variable is changed in scope, the function or local variable will not be destroyed.
The above is the entire content of this article. I hope you can find some help in the js recycling mechanism.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!