JS Closed Package

Source: Internet
Author: User

The concept of closures has been very vague, what exactly is closure?

// function nesting functions, intrinsic functions can refer to parameter variables of external functions, parameters and variables are not reclaimed by garbage collection mechanism, because the internal function BBB () refers to the variable of AAA () function aaa (a) {   var b = 5;    function BBB () {         alert (a);         Alert (b)}}aaa (); // the garbage collection mechanism in JS function aaa () {   var a = 1;}  AAA ();
// a simple closure example. Decision A is not reclaimed by the garbage collection system function  aaa (    ) {var a = 5;     function BBB () {        alert (a)  }
//define a global variable A to implement the additive functionvarA = 1;functionaaa () {a++;     alert (a);   }alert (a); //2alert (a);//3//defines a private variable A that cannot be accumulatedfunctionaaa () {varA = 1; A++; Alert (a)}alert (a);//2alert (a);//2//the benefits of closures, even if a is not a global variable can be accumulatedfunctionaaa () {varA = 1; return function() {a++; Alert (A)}}varc =aaa (); C ();//2C ();//3Alert (a)//I can't find the error .//function declarations and function callsfunctionaaa () {alert (a)}aaa ();//function Expression(functionaaa () {Alert (1)})()//rewrite closures, modular code, reduce pollution from global variablesvarAAA = (function(){  varA = 1;return function() {a++; Alert (A)}) () AAA ()//2AAA ()//3//the existence of private membersvarAAA = (function(){   varA = 1;//Local Variables   //BBB (), CCC () Private Method   functionBBB () {a++; Alert (a)}functionCCC () {a++;  alert (a); }  return{b:bbb, C:CCC}}) (); aaa.b ();//2AAA.C ();//3alert (a);//ErrorAlert (BBB);//ErrorAlert (CCC);//Error
//find the index of the corresponding element in the loop<script>varALi = document.getelementbytagname (' li ');//Method 1   for(vari = 0; i<ali.lenght; i++){       (function(i) {Ali[i].onclick=function() {alert (i); }}) (i)}//Method 2 for(vari = 0; i<ali.lenght; i++) {Ali[i].onclick=function(i) {return function() {alert (i); }}) (i)}</script><body><ul><li>111111</li><li>111111</li><li>111111 </li></ul></body>

returnvar c=aaa (); C ()
<script>//memory leak caused by IEvarOdiv = Docudment.getelementbyid (' Div1 ')); Odiv.onclick=function() {Odiv.onclick=function() {alert (odiv.id);};//Workaround OneWindow.onunload =function() {Odiv.onclick=NULL; }}</script><script>//memory leak caused by IEvarOdiv = Docudment.getelementbyid (' Div1 ')); Odiv.onclick=function(){varOdiv =odiv.id; Odiv.onclick=function() {alert (odiv);};//Workaround TwoOdiv =NULL;}</script><body><div id= "Div1" >aaa</div></body>

Closure of the video, found on the internet to speak very well: http://v.youku.com/v_show/id_XNzkwNzM4NTc2.html

JS Closed Package

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.