Understand the JS recycling mechanism popular easy to read _javascript skills

Source: Internet
Author: User

Before the article, explained JS in the recycling mechanism, but for me at that time, my own recycling mechanism of this concept is also somewhat ignorant, now the recycling mechanism has a more in-depth understanding, so I hereby release this article to the summary, or deepen the memory.
Why should there be a recycling mechanism? Why

For example, I have a memory card, this memory is 8G, I put files, video, music, all saved to this memory card, as my storage content more and more, this memory card has not been saved, if I want to save other files to this memory card will need to delete some files, But these deleted files are our own manual delete, right, manual deletion is equivalent to JS in the delete.

These problems also arise in these programming languages, yes, memory! Any variables we declare need to consume memory, and the more these variables run, the slower it will be. Of course not just variables, 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 are not recycled.

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

3. Will not be recycled as long as it is referenced by another scope

I use a few examples to prove this.

function A () {
 var user = "Dream child";
 return user;
}
var B = A ();

I was supposed to be unable to access the variables in function A, but I received the value of the function a return via global variable B, so the last piece of code became the following.

function A () {
 var user = "Dream child";
 return user;
}
var B = "Chasing The Dream Son";
Console.log (b);

As if this doesn't see any code recycling, let's look at the next piece of code.

The function A () {
 var num = 0;
 return function () {
  num + +;
  Console.log (num);}
 ;}
var B = A ();
b (); 1
B ();//2
B ();//3

You see, if the normal practice, then the output should be 3 times 1 pairs, because the function body once run then the body of the function of the code will be emptied, since it will be emptied so the next time you run this code, NUM should still be 1, but here the situation is somewhat different, As I said above, a local variable in a function is referenced by another scope and the code is not destroyed.

The code above is like this.

The 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 function A is shifted to window by function A, which is not destroyed because the anonymous function is referenced by the global variable B.

The function A () {
 var num = 0;
 return function () {
  num + +;
  Console.log (num);}
 ;}
var B = {
 fn:a ()
}
B.fn ();//1 B.fn ();//2 B.fn
();//3

The

is also possible, because the anonymous function is referenced by the property fn of object B, changing its scope, simply as long as a function or local variable is scoped so that the function or local variable is not destroyed.
The above is the entire content of this article, I hope you can help the JS recycling mechanism.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.