Talk about my understanding of JS closures

Source: Internet
Author: User

This blog to undertake on an article, if you do not see a piece, suggest to look ... Train..... Well, let's take a look at this closure, and this time our focus is not on figuring out what closures are. But to figure out how JS's closure was produced. Then an example of the previous blog post:

var function (x) {    var b = ' BB ';     var function () {        var c = ' cc ';    };     return b;};

When the A function is executed to assign an anonymous function to the inner variable, the following reference relationship is formed, directly reusing the last blog diagram:

It is easy to see that at this time the [[Scope]] intrinsic property of the Inner function object refers to the active object of function A during the execution period (which contains all local variables of the A function) and the global scope. If we pass this inner function at this time, the following code:

var function (x) {    var b = ' BB ';     var function () {        var c = ' cc ';    };     return inner;}; var closure = a ();

This code means: Execute a function, return the inner function, and assign it to the closure variable, when the global variables closure and inner variables refer to the same function, you can run the following code:

var function (x) {    var b = ' BB ';     var function () {        var c = ' cc ';         = = = closure);    };     return inner;}; var closure = A (); closure (); /* Output console: True */

This code confirms that the variables closure and inner refer to the same function object, in other words, the function named inner is the same function as the function named closure, and the scope chain of the closure function is, of course, the same as the inner function, such as:

At this time to combine the previous blog, we should understand why, when a function returns, the closure function can also use the inside of a function local variables.

There is also a problem, that is, analogy with other languages, such as C, when a function is completed after the return, should release all the memory resources of the function execution period, of course, also contains its local variables, if not, will not cause memory leaks? The JS interpreter will certainly be released, but the JS interpreter does this: when the function is finished, the garbage collection mechanism of the JS interpreter is used to free the memory, and its working mechanism is basically this: it will be executed on a regular basis to determine if a block of memory is still referenced by the available variables, and if not, to free the memory directly. If there is, it will not be released. In fact, we are writing JS code is no way to directly destroy the memory space occupied by the object, the release of memory basically rely on JS garbage collection mechanism. Then someone would say the delete operator, and the delete operator cannot destroy the memory space occupied by the object, as in the following example:

var a = {k: ' Consume Memory '}; var b = A; Delete A;console.log (b); /* {k: ' Memory-consuming '} object not released */ Console.log (a); /* syntax error, A is not defined */

So what exactly did delete do? It destroys the a variable so that the original variables A and B both refer to the memory area occupied by the {k: ' Memory '} object, and now only B refers to the memory area when a is destroyed. If we perform the next operation again:

delete b;

At this point, there is no available variable to reference {k: ' Memory '} objects occupy the memory area, the garbage collection mechanism will naturally identify and release resources.

Back to the topic of closures, if the closure is not generated, a function after the execution. The execution period context of a function will naturally be released by the garbage collection mechanism, and the local variable inner function object inside the A function will also be freed, which undoubtedly leaves no memory leaks. However, if the inner function object is returned as a result and assigned to closure, resulting in a closure, the function object will not be destroyed, and the scope chain referenced by its internal variable [[Scope]] will certainly not be destroyed. It all happened so spontaneously ... ^_^.

But JS's closure, if used improperly, is very easy to cause memory leaks, but in view of its benefits, we have to use it, but also to avoid causing memory leaks, so it is important to understand deeply ^_^

All right, that's all for today. If there is an error, please tap .....

Talk about my understanding of JS 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.