In layman's JavaScript closures

Source: Internet
Author: User
Tags ming

Garbage collector

I personally call it the "function to block the garbage collector" or "a function that has access to another function's internal variables" (of course, this is my own way of understanding, everyone may have different ways of understanding), why do you say so? That's a lot to talk about. Garbage collector, some programming languages such as C language for memory management is the programmer's decision, that is, the programmer determines whether the variable needs, if not necessary to release the space occupied by the variable, and this variable is not there, the advantage is that the memory space can be released in a timely manner, Use a program that needs to use memory space.

In JavaScript, we don't need to do this, and JavaScript has a system called the "garbage collector" that automatically recycles variables that we no longer use. That's the question, how does it know we don't need this variable anymore? In JavaScript, the mechanism for implementing garbage collection is called "Reference Counting" (and of course, before that there is a "tag purge" that has been discarded, where it is, focusing only on the current thing), and the garbage collector is judged by the invocation (reference) of the variable in the code. If the reference count for this variable is 0, then it means that it can be recycled.

How does reference counting work? Let's take an example:

"There is a company of Xiao Ming, he work is related to sales, so he will open the company's car every day to meet customers, pick up customers to promote." And the steps to get the bus to the Tube car Wang uncle to make an appointment, and then Wang will make the appointment record to today's reservation form, use after the car to the King Master and cut off the record. One day Xiao Ming needs to use the bus, Xiao Ming called Uncle Wang to show that he will use the bus two o'clock in the afternoon, Wang uncle then the appointment record to today's reservation form, to the agreed time, Xiao Ming took the bus. After a meeting with the customer after the bus back to the king Uncle Wang, Wang also on the appointment form of xiaoming on this record to cross off, Wang uncle then looked at the reservation form, today no one to make an appointment, it seems that can work early, so the car stopped back to the garage to work. ”

In fact, the above example can be seen, Wang uncle equivalent to "garbage collector", and Wang Ye based on what to deal with the recovery? is based on the reservation form, and this reservation form is equivalent to "reference count", when no one on this reservation form to book a bus, Uncle Wang took the car back to the garage, of course, if there is an appointment Wang uncle still have to wait for the reservation form without any appointment to stop the car back to the garage and work. This variable can be "recycled" when no one is in need of it.

What is a closure package?

Under the normal recycling mechanism, when we no longer use this function or variable, it will be recycled, no longer use refers to the current state of execution after we do not appear in the call (reference) This function or variable statements, such as functions, after execution if no longer call will be recycled. Here is an example of a simple description of a closure, an example of Loghello is assigned a function returned from the execution of an anonymous function, the function is to have access to the self-executing anonymous function inside the log_num variable, such a function is called closure, Because there is a reference inside the returned function to the self-executing log_num variable inside the anonymous function.

1 varLoghello;2 3 4Loghello = (function () {5 6     varLog_num = 20;7 8     return function() {9 Ten            returnLog_num; One  A     }; -  -} )();

Here is an example of an object that Loghello is assigned to return from the execution function, which contains two methods "read" and "write", both of which are closures, and they all have access to the Log_name variable. "Read" is responsible for reading Log_name the value of this variable, while "write" is responsible for overriding the value of Log_name, in the outside there is no direct access to the Log_name variable, only through the two methods to indirectly access and set the variable, such a method is also a closure.

1 varLoghello;2 3Loghello = (function () {4     5     varLog_name = ' Wyz ';6 7     return {8Readfunction () {9             returnLog_name;Ten         }, One  AWritefunction(newvalue) { -Log_name =newvalue; -         } the     }; -  -} )();

How are closures formed?

From the above know whether a variable can be recycled, according to the reference count of the variable can be judged, for example, the following example, the local variable Log_name reference count is 1, indicating that there is a reference, so the garbage collector will not reclaim this variable, which is the formation of closures.

1 varLoghello;2 3Loghello = (function () {4 5 6     varLog_num = 20;7 8     return function() {9 Ten            returnLog_num; <<returns an anonymous function in which local variable log_num can be accessed inside the function.  One  A     }; -  -  the} )();

After some of the above examples, it is easy to understand, OH ~ has not been understood? , then you can continue to look at the following summary (but believe me, think more, you will understand).

With regard to closures, it can be said that a function that has access to another function's internal variable and that the function will not be recycled as it executes, because a reference to the function's internal variable exists in the returned closure function.

In layman's JavaScript closures

Related Article

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.