JavaScript Learning Log (iii): Closures

Source: Internet
Author: User
Tags closure

To tell you the truth, the previous section of the prototype and prototype chain in the beginning of the study did not have a headache, the third version of the advanced programming to understand a few times, the closure of this section is really a headache, very ashamed, looked at almost ten times, but also looked at the other online blog and explain the document, a variety of expressions, although the core ideas are consistent , but it is not able to do their own understanding, and then the combination of functional chain, finally a bit enlightened, while hot, hurriedly write down, interested can refer to.

Closures:
Advanced programming The above explanation refers to a function that has access to a variable in another function scope (is a function);
A common way to create closures is to create another function inside one function.

Before you understand closures, be aware of a sequence of functions that are created to the call and then to the end:
when a function is called, an execution environment and the corresponding scope chain are created, and then the active object of the function is initialized with the values of the arguments object and other named parameters, but in the scope chain, the active object of the outer function is always ranked second. The external function of the outer function of the active object is ranked third ... The last one is the active object under the global execution environment. (one lap to the outside, like the target of archery, the middle of the heart is the current execution environment of the active object)
Take a function as an example:


function Compare (value1, value2) {
Return ...
}
var result = Compare (5, 10);


when the function compare () is created, a scope chain is created that contains the global variable object, which is stored in the internal [scope] (or scope) attribute, and when the Compare () function is called, an execution environment is created for the function. It then builds the scope chain of the execution environment by copying the objects in [Scope], and then pushes the active object to the front end of the execution environment scope chain. At this point, there are two variable objects on the scope chain of compare (), the first is a local variable object: Arguments,value1,value2, and the second is a global variable object: Result,compare. When the function is finished, the local active object is destroyed, leaving only the variable object under the global execution environment.

but closures are different:
If a function is defined inside a function, the inside function adds the active object of the outer function to its scope chain, but its scope chain is the front end of its own active object, followed by the active object of the external function, the active object of the external function of the external function .... Variable objects until the global execution environment.
function Compare (value1, value2) {
return function (name) {
... ..
}
}
var = compare (Comparea);
var result = Comparea ("aaaa");
when an anonymous function is returned in compare (), its scope chain is initialized, containing the active object of compare () and the global variable object. When compare () finishes executing, its active object is not destroyed because the scope chain of the anonymous function is still referencing the active object. That is, after the compare () function returns, the scope chain of its execution environment is destroyed, but its active objects remain in memory until the anonymous function is destroyed and its active object is destroyed. With the above example, when executing compare, if there is no closure, the execution environment of compare will be destroyed after execution, the active object will not remain in memory, only the global variable object is left in memory, but if there is a closure inside the function, the inside function returns , when the scope chain of the intrinsic function is initialized, the active object outside the inner function (that is, the active object of the Compare) and the global variable object are in memory, so that the active object of the compare can still be accessed, not destroyed, Although the scope chain of compare at this time has been destroyed.

=====

Make a summary:

When a function is created, it also produces a scope chain, which is entirely new, there is no current active object, only the global object, this scope chain is the core chain, stored in the internal "scope" property, always in, when the function is called, Will first create an execution environment (about the execution environment, the activity object, the variable object these nouns refer to the first essay), and then copy the objects in the "scope" just inside to build a scope chain of the execution environment, which is not the same as the scope chain that was saved when the function was created, which is specially prepared for the execution environment. So the current variable object, that is, the active object will be top to the front of this scope chain, at this time there are two variable objects on this chain, the first is the current active object, followed by the global environment defined variable object, if there is no closure, when the function is finished, the current execution environment will be destroyed, At the same time, the currently active object is destroyed, and only the variable objects in the global environment are left on the scope chain. In the first article, we talked about how to access a variable, and the identifier parsing is a layer-by-level search along the scope chain. So this time we have to access the current variable object is no way.

At this time the function of the closure is there.

A new function is returned inside the function, when the new function is called, then a new execution environment is created for the new function, and the variable object (also the active object) under the current execution environment is pushed to the front of the scope chain, and the variable object in the execution environment of the outer function is squeezed to the second one. Since the outer function has been executed, the corresponding execution environment has been destroyed, but its execution environment of the variable object is still in this scope chain, in the second, so we can still access through this scope chain of the function has just been executed in the execution environment defined by the variable object, Although it has been executed and has been destroyed.

I'll make a further summary:

The function of closures: variable objects that have access to intrinsic functions,

Implementation method: Return a new function inside the function

Implementation principle: On the scope chain, in front of the variable object to be accessed and then plug a new variable object, you can ensure that the variable object to be accessed is not destroyed and can be accessed. (Just like a string of candied fruit, you worry that the top one may fall off, then a string on the top of it, you can guarantee that the second one will not fall)

=====


Closures and variables:
closures can only get the last value of any variable in the containing function (external function). Closures save the entire variable object, not a particular variable.
Here are the classic cases:
function Createfunctions () {
var result = new Array ();
For (var i = 0; i<10; i++) {
Result[i] = function () {
return I
}
}
return result
}
each function returns 10 because the active object of createfunctions () is stored in the scope chain of each function, and its active object is I, and when Createfunctions () is executed, I has a value of 10. So the internal I in the scope of each function is 10. This is passed by reference and is passed by value if you want to achieve the results we expect.
function Createfunctions () {
var result = new Array ();
For (var i = 0; i<10; i++) {
Result[i] = (function (num) {
return function () {
return num
}
}) (i)
}
return result
}

About this object:
The execution environment of an anonymous function is global, so its this object usually points to window. So the this in the closure is usually under the global variable object.

JavaScript Learning Log (iii): Closures

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.