JavaScript for loop closure "Go"

Source: Internet
Author: User

A netizen asked a question, such as the next HTML, why each output is 5, instead of clicking on each p, alert out the corresponding 1,2,3,4,5.

  1. <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
  2. <title> Closures Demo </title>
  3. <script type= "Text/javascript" >
  4. function init () {
  5. var pary = document.getElementsByTagName ("P");
  6. for (var i=0; i<pary.length; i++) {
  7. Pary[i].onclick = function () {
  8. alert (i);
  9. }
  10. }
  11. }
  12. </script>
  13. <body onload= "Init ();" >
  14. <p> Product One </p>
  15. <p> Products Two </p>
  16. <p> Products Three </p>
  17. <p> Products Four </p>
  18. <p> Products Five </p>
  19. </body>

There are several ways to solve the problem

1. Save the variable i to each paragraph object (p)

    1. function init () {
    2. var pary = document.getElementsByTagName ("P");
    3. for (var i=0; i<pary.length; i++) {
    4. PARY[I].I = i;
    5. Pary[i].onclick = function () {
    6. alert (THIS.I);
    7. }
    8. }
    9. }

2. Save the variable i in the anonymous function itself

    1. function Init2 () {
    2. var pary = document.getElementsByTagName ("P");
    3. for (var i=0; i<pary.length; i++) {
    4. (Pary[i].onclick = function () {
    5. alert (ARGUMENTS.CALLEE.I);
    6. }). i = i;
    7. }
    8. }

3, add a layer of closure, I in the form of function parameters passed to the inner layer function

    1. function Init3 () {
    2. var pary = document.getElementsByTagName ("P");
    3. for (var i=0; i<pary.length; i++) {
    4. (function (ARG) {
    5. Pary[i].onclick = function () {
    6. Alert (ARG);
    7. };
    8. }) (i);//call-time parameter
    9. }
    10. }

4, add a layer of closure, I in the form of local variables passed to the memory function

  1. function Init4 () {
  2. var pary = document.getElementsByTagName ("P");
  3. for (var i=0; i<pary.length; i++) {
  4. (function () {
  5. var temp = local variable when i;//called
  6. Pary[i].onclick = function () {
  7. Alert (temp);
  8. }
  9. })();
  10. }
  11. }

5, add a layer of closures, return a function as a response event (note the subtle difference from 3)

    1. function Init5 () {
    2. var pary = document.getElementsByTagName ("P");
    3. for (var i=0; i<pary.length; i++) {
    4. Pary[i].onclick = function (ARG) {
    5. return function () {//Returns a functional
    6. Alert (ARG);
    7. }
    8. } (i);
    9. }
    10. }

6, with the function implementation, in fact, each generation of an instance of the functions will produce a closure

    1. function Init6 () {
    2. var pary = document.getElementsByTagName ("P");
    3. for (var i=0; i<pary.length; i++) {
    4. Pary[i].onclick = new Function ("alert (" + i + ");"); /new one function instance at a time
    5. }
    6. }

7, with the function to achieve, pay attention to the difference with 6

    1. function Init7 () {
    2. var pary = document.getElementsByTagName ("P");
    3. for (var i=0; i<pary.length; i++) {
    4. Pary[i].onclick = Function (' Alert (' +i+ ') ')
    5. }
    6. }
this blog goes from other blogs

JavaScript for loop closure "Go"

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.