javascript--Closure Package

Source: Internet
Author: User

I. Characteristics of closures and the relationship between closures and garbage collection mechanisms
1<script>2         /**3 * Characteristics of closure structure4 * functions nested functions5 * The nested function has a return value, and the return value calls the variable of the outer function .6          **/7 8 9         /**Ten * Garbage collection mechanism One * Memory is used when variables are created A * Garbage collection mechanism is recycled (removed from memory) by unused variables and exhausted variables -          * */ -  the         functionfn1 () { -             //Local Variables -             vardata1 = "Data"; -             functionfn2 () { +                 returndata1; -             } +             returnfn2; A         } at  -  - //Console.log (FN1 ());//Output FN2 -  -         //This can output the data stored in local variables, indicating that the garbage collection mechanism does not recycle local variables -         //Cons: Consuming memory (local variables not being deleted) in         //benefit: Local variable data1 does not pollute external variables -Console.log (FN1 ());//"Data" to</script>
Two. Examples of non-closures
1<script>2         vararr = [10,20,30];3 4         //The usual for loop I creates within the parentheses of the for5        /*For (var i = 0; i < arr.length; i++) {6 Arr[i] = function () {7 Console.log (i);8           }9 Ten         } One Arr[0] ();//Output 3*/ A  -  -         //This is the equivalent of creating an I variable on the outside, which is used in the For loop. the /*var i = 0; - For (; i < arr.length; i++) { - Arr[i] = function () { - Console.log (i); +             } -  +         }*/ A  at //Arr[0] ();//Output 3 At this moment of execution, ask how much I was - //alert (i);//Output 3 -  -  -         //--------------------Example 2------------------------- -Console.log (111); in         vari = 0; -         //The for loop was executed at the moment we released it. to          for(; i < 3; i++) { +           //The timer actually executes at the last minute of the For loop. -SetTimeout (function () { theConsole.log (i);//Output 3 *},1000); $         }Panax Notoginseng  -Console.log (222); the</script>
Three. Examples of differences between closures and non-closures
1<script>2         //does not form a closure, the function inside does not use the external function variable3  /*var name = "The window";4 var object = {5 Name: "My Object",6 getnamefunc:function () {7 Console.log (this);//Get Object8 return function () {9 //What is this here?Ten Console.log (this);//At the moment the call becomes window One return this.name; A                 } -             } -         }; the  - Console.log (Object.getnamefunc ()); - //Object.getnamefunc () run to get function*/ -  + //form a closure, the function inside uses the external function variable -  varName = "the window"; +  varObject = { AName: "My Object", atGetnamefunc:function () { -          //Save the external this to the local variable. -         varthat = This; -          return function () { -              //a local variable was called by a nested function, which forms a closure, -              //so that variable is not recycled and keeps the outside object.  in              returnThat.name; -          } to      } +  }; -  the Console.log (Object.getnamefunc ()); *</script>

javascript--Closure 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.