JS Closure (multi-image, mobile side of caution point)

Source: Internet
Author: User

Recently in See JS closure, there is a place to see for a long time have not understood.

1     functioncreatefunctions () {2         varresult =NewArray ();3          for(vari = 0; I < 10; i++) {4Result[i] =function () {5                 returni;6             }7         }8         returnresult;9}

The book says that each function returns 10, I think the solution, always feel that the return should be 0,1,2,3....9. So I wrote the code to test a bit, and sure enough, are all ten.

1     var getresult = createfunctions (); 2     Console.log (getresult); 3      for (var i = 0; i < i++) {4         var tt = getresult[i]; 5         Console.log (TT ()); 6     }

At the time of the first write, the TT in Console.log () was not parenthesized, and the result was 10 identical functions.

Each function body is a return i; I suddenly realized that in the Createfunctions function, only an anonymous function with the same function body was assigned to each array element, and no function was executed. The real need to get these function values is to execute the function. And when these functions are executed, the function of Createfunctions is destroyed, leaving only I this activity object is saved in the scope chain of the anonymous function, and the value of I is 10. Therefore, the execution result of 10 functions is 10.

What about the elements in the array, respectively? At this point, I immediately assign the result of the anonymous function inside the createfunctions function to the elements in the array, the code is as follows:

1     functioncreatemyfunctions () {2         varresult =NewArray ();3          for(vari = 0; I < 10; i++) {4Result[i] = (function () {5                 returni;6             })();7         }8         returnresult;9     }Ten     varGetmyresult =createmyfunctions (); OneConsole.log (Getmyresult);

But in this case, the elements in the Getmyresult array are [0,1,2,..., 9]. If you want the elements in the array to still be 10 functions, the return value of the function is an index, which is written in the book:

1     functioncreatetheirfunctions () {2         varresult =NewArray ();3          for(vari = 0; I < 10; i++) {4Result[i] = (function(num) {5                 return function () {6                     returnnum;7                 };8 }) (i);9         }Ten         returnresult; One     } A     varGettheirresult =createtheirfunctions (); - Console.log (gettheirresult); -      for(vari = 0; I < 10; i++) { the         vartt =Gettheirresult[i]; - Console.log (TT ()); -}

The above is the use of nested functions, and another way is to use the WITH statement to create a block-level scope:

1     functioncreateyourfunctions () {2         varresult =NewArray ();3          for(vari = 0; I < 10; i++) {4             vart ={value:i};5              with(t) {6Result[i] =function () {7                     returnvalue;8                 };9             }Ten         } One         returnresult; A     } -     varGetyourresult =createyourfunctions (); - Console.log (getyourresult); the      for(vari = 0; I < 10; i++) { -         vartt =Getyourresult[i]; - Console.log (TT ()); -}

Of course with and not I think out of the way, is a big God to come out, listen to the big God a lot of analysis, I deeply JS abstruse!

The following is a transcript of the chat with the Great God (study how to Output 0-9):

JS Closure (multi-image, mobile side of caution point)

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.