The closure of JavaScript is a subsidiary.

Source: Internet
Author: User
Tags closure

In computer science, closures (Closure) are short for lexical closures (Lexical Closure) and are functions that reference free variables. This quoted free variable will exist with this function, even if it has left the environment in which it was created. So, there is another argument that closures are entities that are composed of functions and their associated reference environment. ---wikipedia

A company will have its own house. "A function has its own scope."

The company will receive some raw materials, do some processing, and return something. "The function receives the parameter, it is processed, and there is a return value"

Outside things, are public, so within the company can also use outside things. "Global variables can be accessed inside the function"

The things inside the house are the company's own. Can only be used within the company, outside can not see also use. "Local variables are functions that are private and can only be used within a function and cannot be accessed externally."

The company can open some laboratories, the laboratory is internal, so you can also use the company's internal equipment. "Function B, defined inside function A, can access the local variable of function a"

Once a company has completed its historical mission, it will be useless, demolished, and the private device inside will disappear. "After the function is executed, it will be recycled by the GC, and the local variables will also die out."

So far everything seems normal, so the point is, what if the company didn't give the product at the end, but instead threw the internal lab out to be another company?

The answer is that the company will still be relocated, but the equipment that the subsidiaries need to use will not be recycled, as the subsidiary needs to. So, this subsidiary is closure, it in the original company ran out before the collapse, continue to survive, but also in the original company's private device affixed a label said "this equipment I also want to use, you put the original company's other things recycling good, this do not recycle", so the demolition Brigade GC will not recycle this equipment.

So we realized that we could use the equipment after a company went bankrupt. "After the function has run, you can access its local variables in some way, the function ends, but the local variable is still there, and the effect of the closure appears."

That is, "closures make variables always in memory" or "closures are entities that are composed of functions and their associated reference environment."

Precautions

When 10 subsidiaries are created within a company, these subsidiaries will only label existing devices, i.e. 10 subsidiaries will share one device

Like this

 for (var i = 0; i < i++) {    setTimeout(function() {        console.log (i);       );}

The result is output of 10 10 instead of 0-9.

Just like the company has a blackboard I.

Write 0 on the blackboard I, and then create an internal company that will send out what is written on this blackboard tomorrow.

Change the 0 on the blackboard I to 1, and then create an internal company that will send out what is written on this blackboard tomorrow.

The 1 on the blackboard I is changed to 2, then create an internal company and let it send out the things written on this blackboard tomorrow.

......

Altogether 10 companies were founded.

Then the company died, but the blackboard I was left behind because there were still 10 subsidiaries to use.

Tomorrow, 10 subsidiaries went to see the blackboard I, which wrote a "10", so they all outward issued a "10."

So what do you do?

 for (var i = 0; i < i++) {    (function(e) {        setTimeout (function () {            console.log (e);           );    }) (i);}

This is different, each subsidiary has its own blackboard "E" (of course, the subsidiary of the Blackboard is also named I, the same can be used, that is, it seems to be more egg pain).

At the time of the establishment, the company's blackboard I on the copy of the things on their blackboard, waiting for the release tomorrow.

So 10 subsidiaries, have their own blackboard, on the blackboard is their creation when the blackboard I on the content, even if the contents of the blackboard I changed, but the establishment of the subsidiary will not go to the tube, he is the home of the Blackboard E.

Tomorrow, 10 subsidiaries have released their own Blackboard E on the content, that is, 0-9, no one to control the original blackboard I.

Almost like this, more specifically can be seen in the details of JS closures and Mozilla's closure tutorial, this is a good two articles.

I also started to learn JavaScript, what is wrong, welcome to point out.

The closure of JavaScript is a subsidiary.

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.