JavaScript closure issues

Source: Internet
Author: User
Tags closure

What is a closure??

A function that has access to a variable within the scope of another function is a closure.

See an example:

1 functionA () {2   varn = 0;3   functionInc () {4n++; 5 Console.log (n);6   }7   returnInc;8 }9 varc =a ();TenC ();//Console Output 1 OneC ();//Console Output 2

Here, the console two times the output is not the same??? Why?? is the variable n not re-assigned??

And no, here, var C = a (); This code means that the return value of the A method is assigned to the variable C, then the return value of a () is Inc, also the Function Inc (),

two times C () after execution; C (); In fact, it is an Inc () that executes two times; Because function always refers to a (), n is not recycled, that is, the value of n is always in use,

Nature first executes C () Printing 1, second execution C () printing 2;

Let's look at a classic example:

1 functioncreatefunctions () {2   varresult = [];3    for(vari=0; I < 10; i++){4Result[i] =function(){5       returni;6     };7   }8   returnresult;9 }Ten varFuncs =createfunctions (); One  for(vari=0; i < funcs.length; i++){ A Console.log (Funcs[i] ()); -}

Generally, the output is 0~9, but the output is in fact 10 10.

The point to note is that the method band () is the only way to execute this method, and for example var abc = function () {}, it can only be said that the method is defined and assigned to ABC, and this method is not executed.

Simply put: a simple sentence var f = function () {alert (' Lalala ');}; is not to play the window, followed by a sentence F (); The code inside the method is executed before the window is bounced.

Explanation of the above code:

1 functioncreatefunctions () {2   varresult =NewArray ();3    for(vari=0; I < 10; i++){4Result[i] =function(){5       returni;6     };7   }8   returnresult;9 }Ten varFuncs = Createfunctions ();//performed a createfunctions () One  for(vari=0; i < funcs.length; i++){ AConsole.log (Funcs[i] ());//The return value of createfunctions is result, so funcs[i] () executes the method of Result[i]: return i; -}

Execution process:

1  varresult =[], I;2Result[0] =function(){returnI };//No function is executed, the function is unchanged inside, and I cannot be replaced within the function! This is the first time the method is executed when assigning createfunction () to Funcs3RESULT[1] =function(){returnI };//No function is executed, the function is unchanged inside, and I cannot be replaced within the function! 4  ...5RESULT[9] =function(){returnI };//No function is executed, the function is unchanged inside, and I cannot be replaced within the function! 6i = 10;//at this time i = ten;7Funcs = result;//because the return value of createfuntions () is result8result =NULL;9    TenConsole.log (i);//Funcs[0] () is the execution of the return I statement, which is the return OneConsole.log (i);//Funcs[1] () is the execution of the return I statement, which is the return A  ... -Console.log (i);//Funcs[9] () is the execution of the return I statement, which is the return

Because I is being referenced, the value of I is 10, which is return i; The value is always return 10. Passed to the Execute Funcs[i] () method inside.

Funcs[0] () is the execution of the return I statement, which is to return 10
FUNCS[1] () is the execution of the return I statement, which is to return 10
FUNCS[9] () is the execution of the return I statement, which is to return 10

A closure is a variable in which a function refers to another function, because the variable is referenced so it is not recycled, so it can be used to encapsulate a private variable. This is the pros and cons, unnecessary closures will only increase memory consumption! also use closures to pay attention to whether the value of the variable matches your requirements, because he is like a static private variable. Closures are often mixed up with a lot of things to get to know more, and this is just a basic thing to talk about.

JavaScript closure issues

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.