Features of JavaScript closures

Source: Internet
Author: User
In the most typical example, no matter which result is clicked, the above Code is the final value, because the click Event receives

First look at the Code:

 
 
  • 1111
  • 2222
  • 3333
Script var a = document. getElementsByTagName ('lil'); for (var I = 0, l = a. length; I

In the most typical example, no matter which result is clicked, the above Code is the final value, because the function received by the click event forms a closure, the I in the closure is only a reference to the variable I in the external function. When fn is executed, the variable I is the final value obtained by the loop, and the variable I in the closure is the value, therefore, the values 1, 2, and 3 are not displayed in sequence.

Solution:

1. Add custom attributes for each element to save the current index value.

Function fn () {var a = document. getElementsByTagName ("li"); for (var I = 0; I

2. Save the current index value to the anonymous function itself.

Function fn () {var a = document. getElementsByTagName ("li"); for (var I = 0; I

3. Add a closure to pass the current index value to the internal function as a function parameter.

Function fn () {var a = document. getElementsByTagName ("li"); for (var I = 0; I

4. Add a closure to pass the current index value as a variable to the inner non-function.

Function fn () {var a = document. getElementsByTagName ("li"); for (var I = 0; I

5. Add a closure to return a function as a response event.

Function fn () {var a = document. getElementsByTagName ("li"); for (var I = 0; I

6. When using the Function object, you must note that the Function constructor creates a Function and uses the parameter as the parameter of the new Function during the script operation. Therefore, the execution efficiency is lower than that of the previous methods.

Function fn () {var a = document. getElementsByTagName ("li"); for (var I = 0; I

7. Use Function object instances to generate closures.

Function fn () {var a = document. getElementsByTagName ("li"); for (var I = 0; I ". length; I ++) {a [I]. onclick = new Function ('alert ('+ I +'); // a Function instance is generated once new }}

This article is available at http://www.nowamagic.net/librarys/veda/detail/290.

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.