Let you understand the closure in one sentence (easy to understand), simple to understand in one sentence

Source: Internet
Author: User

Let you understand the closure in one sentence (easy to understand), simple to understand in one sentence

I have been using javascript For A Long Time. Every time I understand the closure, it seems like nothing. Recently I am looking for the work of the Web Front-end, so I need to consolidate the foundation.

This article referencesJoy_leeBlogClosureOn the basis of her blog post, I strive to explain my understanding in the form of comments. If there are any mistakes, please criticize and correct them.

In advanced programming, the closure is created when other functions are defined within the function. The closure has the right to access all variables within the function.

(How can this sentence be understood? According to this sentence, closure is a nested function! Nested functions can certainly access the variables of the functions that contain them. This is no problem .)

In general, internal functions can access variables at the upper level or even the global level, so some people will understand that through closures, external variables in the local area of the function can be accessed.

(If we divide the scope into several levels, assume that the global scope is the first level, and the internal scope of the defined function body is the second level, the internal scope of the function body defined in the second-level scope is the third level ,.... in the traditional sense, level 1 cannot access level 2 variables (such variables are called local variables), and level 2 cannot access level 3 ,..., in turn, this is the scope chain. Within the scope of this level, you cannot find it at the upper level until the first level is global. The closure mechanism can reference the variables in the second-level scope through the third-level scope in the first-level scope, the method is to return a function reference with a third-level scope to the first-level scope in the second-level scope. This reference is the key, because the reference exists, the related third scope and second scope become the context of the reference operation, the garbage collection mechanism GC cannot recycle the resources occupied by this chain. If this reference is not provided, resources after the function is run will be reclaimed, just like normal functions. My doubt is that the closure refers to the nested function in a single function or the nested function referenced by the first level? Or are they all? Or is this mechanism formed when a nested function is referenced by the first-level scope rather than a nested function ?)

function a(){     var i=0;    function b(){       alert(++i);      }     return b;   }   var c = a();   c();

That is, the function of the closure is that after a executes and returns, the closure makes the garbage collection mechanism of Javascript GC not to reclaim the resources occupied by, because the execution of the internal function B of a depends on the variables in.

Because of the existence of the closure, the I in a always exists after function a returns, so that each execution of c (), I is the value of alert after the auto-increment of 1.

Then, if a does not return function B, the situation is completely different. Because after a is executed, B is not returned to the external world of a, but is referenced by a. At this time, a will only be referenced by B, therefore, functions a and B are referenced by each other without being disturbed by the outside world (referenced by the outside world), and functions a and B are recycled by GC.

In fact, it is the closure that extends the life cycle of the variable. Generally, the function scope means that the variable will be destroyed after the function execution ends. However, when the function returns a closure, the entire scope chain will occupy the memory as long as the closure is not released. (Closure extends the lifecycle of a variable, which is referenced by the first level. But without this reference, can a closure still be called a closure ?)

Scope chain: the scope of the function, the scope of the function at the previous layer, and the global scope. When you access a variable, if you do not have it, you can find it all the way up until it is global. If not, an error is returned.

(The scope chain of the closure is bent .)

PS: Some netizens recommended another articleSimple Analysis of the concept of javascript ClosureWhether the authority is not mentioned for the moment, there is a clear concept:

After talking about so much, what is the closure? The following is a summary:

Closure is a concept that describes the phenomenon of memory resident after the function execution is completed and the memory is released. It is hard to understand the closure as long as you grasp this core concept.

The above Article makes you understand the closure in one sentence (simple and easy to understand), which is all the content shared by the editor. I hope to give you a reference and support for the help house.

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.