Closed Pack Comb!!

Source: Internet
Author: User

Has this concept been heard countless times? A bite to answer?

What is it, a closed bag?

Official concept: Closures refer to functions that have access to variables in another function scope!!!

Three keywords: Right! Another function in the scope of the variable! Function!

Right? How is it implemented?

The parent function scope chain is destroyed, but the active object is referenced by the closure and still resides in memory, which can be assigned a null destroy.

About classic JS Loop closure comprehension

Click Requires alert (i) This I is not defined within the anonymous function, so to query within the parent and function, get 4. So you'll alert4.

 for (var0; i++) {    setTimeout (function () {        console.log (i);      + );}

This example is also the same, I get the value of 10;

How to solve?

Prior to understanding closures. It is better to understand the meaning of the scope chain, simply speaking, the scope chain is the function at the time of definition created, to find the value of the variable used in an index, and his internal rule is to put the function of its own local variables in the first, the parent function of its own variables in the second, Put the variables in the higher-level function later, and so on until the global object. When the function needs to query the value of a variable, the JS interpreter will go to the scope chain to find, from the first local variable to find, if not found the corresponding variable, then to the next level of the chain to find, once the variable is found, will no longer continue. If the required variable is not found at the end, the interpreter returns undefined.

Understanding the scope chain, we look at the JS memory recovery mechanism, in general, a function at the beginning of execution, it will be defined in the variable memory space to save, in case of the subsequent statements to use, wait until the function is finished to return, These variables are considered useless. The corresponding memory space is also recycled. The next time the function is executed, all the variables return to their original state and are re-assigned. But if another function is nested inside the function, And this function is likely to be called externally. And this internal function uses some variables of the external function. This memory-recycling mechanism can cause problems. If an intrinsic function is called directly after the external function returns, the Then the intrinsic function cannot read the value of the variable in the external function that he needs. So the JS interpreter will automatically save the function and the variables he or she may use, including the local variables and the variables (free variables) of the parent and ancestor-level functions, when it encounters the function definition. That is, to build a closure, These variables will not be reclaimed by the memory collector, but only if the internal function is not possible to be called (for example, if it is deleted, or if there is no pointer), the closure will be destroyed, and no one of the closure references will be recycled when the next memory recycle starts.

Closed Pack Comb!!

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.