JS closure causes the loop to add an event to the button always executes the last

Source: Internet
Author: User

Add the following script code:

  1. <script>
  2. var list_obj = document.getelementsbytagname (' Li ');
  3. for (var i = 0; I <= list_obj.length; i++) {
  4. List_obj[i]. onclick = function () {
  5. alert (i);
  6. }
  7. }
  8. </Script>



After running, the strange discovery no matter what click on the Li tag, alert out is the last content, 5

The following analysis: Because the event handler specified to List_obj[i].onclick in the For loop, the onclick anonymous function is called when the For Loop executes (when the user clicks the link). When called, the variable I needs to be evaluated, the resolver is first found inside the event handler, but I is not defined. Then, to the outside of the method to find, there is a definition, but the value of I is 4 (only I greater than 4 will stop execution for the For loop). Therefore, the value is obtained-this is the result of the closure (anonymous function) to use the variables in its outer scope. Also, this is due to the fact that the anonymous function itself cannot pass parameters (and therefore cannot maintain its scope).

Now the reason is to know, how to avoid this situation?

Now that we know that the function calls the external variable, it forms a closure, and the variables are affected elsewhere, so we

Now all you have to do is build a closure that is accessible only to itself, and save the variables that are used only by itself.

Building a closure is simple and the code is as follows:

Way One:

[JavaScript]View PlainCopy
  1. var list_obj = document.getelementsbytagname (' Li ');
  2. for (var i = 0; I <= list_obj.length; i++) {
  3. <span style="White-space:pre" > </span>list_obj[i].onclick = (function (i) { //outer function
  4. <span style="White-space:pre" > </span>return function () { //inner function
  5. <span style="White-space:pre" > </span>alert (i);
  6. <span style="White-space:pre" > </span>};
  7. <span style="White-space:pre" > </span>}) (i);
  8. }*

Way two:

[JavaScript]View PlainCopy
  1. var list_obj = document.getelementsbytagname (' Li ');
  2. for (var i = 0; I <= list_obj.length; i++) {
  3. (function (i) {
  4. //var p = i
  5. List_obj[i].onclick = function () {
  6. alert (i);
  7. }
  8. }) (i);
  9. }

JS closure causes the loop to add an event to the button always executes the last

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.