JavaScript closures (essays)

Source: Internet
Author: User

closures, great closures .... First look at the encyclopedia of the definition of Baidu is what.

The encyclopedia says: closures refer to blocks of code that can contain variables that are free (not bound to specific objects), that are not defined within this block of code or in any global context, but are defined in the context in which the code block is defined (local variables).

It's no good watching the whole Person. What the hell is all this???

or summarize yourself:

What can I do to generate closures? At the technical level of javascript, it is possible for a parent function to have a nested function in the form of a closure Function. As in this case:

The closure function fn creates function fn () {var a = 2;  var B = 4;  function fn1 () {console.log (a);  };     FN1 (); }FN ();

  

Resolution: fn1. [[scope]]--fn.lexical Environme | | fn.[[scope]]//

Personal understanding of Closures:

When you create a fn1 child function, you give fn1 a scope attribute, which points to the lexical environment of the FN parent Function. If the FN function also has a parent function, the scope property of FN points to the lexical environment of the Function's parent function, thus forming a scope chain.

So the nature of closures is because JS supports this scope chain and also supports function nesting functions to cause closure possibilities . Well.. That's the reason!!!

1. Any free variable that does not call the parent function inside the child function does not have a closure at this time;
2. Any free variable of the parent function of the parent function within the child of the child function will produce a closure at this time;

Such as:

First Case: no closures are generated
FUNCTION fn () {var j = 4;    function fn1 () {console.log ("any thing within the subroutine code that does not have a calling function, does not produce a closure;")};  FN1 ();  }; FN ();
The second case: generating the closure function fm () {var k = 5;      function Fm1 () {var y = 9;      function fm2 () {console.log (k) //this function invokes a variable inside the parent function of the parents with a closure; closure-k:5};    FM2 ();    }; FM1 ();
}; FM ();

A simple example of closures:

Calculates the sum of the base and max, but Max adds the value from 11 to Max and then adds it to the base, which reduces the number of global variables defined by the Closure. function Calfu (base) {return function (max) {va    R total = 0;  For (var i =1; i <= max; i++) {total = Total + i;  } return total + base; };}; var reult = Calfu (2), reult (3)  //1+2+3+2 = = 8alert (reult (3));  The result of the popup is 8

One last Sentence:

"because the closure carries the scope of the function that contains it, it consumes more memory than the other functions." Excessive use of closures can lead to excessive memory consumption, and we recommend that readers consider using closures only when absolutely necessary. Though like

The optimized JavaScript engine, such as V8, tries to reclaim the memory that is consumed by the closures, but you should use closures with Caution. ”

Well. Right. This is what the great god Said. cautious, cautious, and cautious ....

JavaScript closures (essays)

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.