Function in loop and Closure

Source: Internet
Author: User

This article describe the famious issue "function in loop and closure" in JavaScript.

The root cause is loop statements (such as for, while) don't have their own scope.

Let's see an example first:

 
<Ul> <li> Item1 </LI> <li> item2 </LI> <li> item3 </LI> </ul>
 
VaR linodes = document. getelementsbytagname ("Li"); For (VAR I = 0; I <linodes. length; I ++) {linodes [I]. onclick = function () {alert ("You click item" + I );};}

Now, if you click each of the list, all will produce a "you click Item 3" alert
Box.

The number 3 comes out of the end execution of the loop (0, 1, 2 and out of
Loop I = 3 ).

Obviusly, the result is not expected.

If you use jslint to validate this piece of code, you will get the following warning:

Be careful when making functions within a loop. consider putting the function in
A closure.

According to jslint's suggest, we have the first solution:

 
// Good-0 Function clicknode (linode, I) {linode. onclick = function () {alert ("You click item" + I) ;}}var linodes = document. getelementsbytagname ("Li"); For (VAR I = 0; I <linodes. length; I ++) {clicknode (linodes [I], I );}

If you don't want to create another function, consider using anonymous funtion:

// Good-1 var linodes = document. getelementsbytagname ("Li"); For (VAR I = 0; I <linodes. length; I ++) {(function (I) {linodes [I]. onclick = function () {// you click Item 0 // you click Item 1 // you click Item 2 alert ("You click item" + I );};}) (I );}

Notice: the self-executing function create a context scope which contains a local
Variable I.

When the click event occurs, the variable I is coming from the closure which is
Just the self-executing function scope.

There are always ways to solve this problem, following are another three ways:

 // good-2 var linodes = document. getelementsbytagname ("Li"); $. each (linodes, function (I, item) {$ (item ). click (function () {// you click Item 0 // you click Item 1 // you click Item 2 alert ("You click item" + I );});}); // good-3 $ ("Li "). each (function (I, item) {$ (item ). click (function () {// you click Item 0 // you click Item 1 // you click Item 2 alert ("You click item" + I );});}); // prefered-4 var linodes = $ ("Li "). click (function (event) {var I = linodes. index (this); // you click Item 0 // you click Item 1 // you click Item 2 alert ("You click item" + I );}); 

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.