On the _javascript techniques of JavaScript for loop closure

Source: Internet
Author: User

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

 
 

There are several ways to solve this problem

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

function init () {   
 var pary = document.getElementsByTagName ("P");   
 for (var i=0; i<pary.length; i++) {   
   pary[i].i = i;   
   Pary[i].onclick = function () {   
    alert (this.i);   
   }   
 }   
}   

2. Save the variable i to the anonymous function itself

function Init2 () {   
 var pary = document.getElementsByTagName ("P");   
 for (var i=0; i<pary.length; i++) {    
  (Pary[i].onclick = function () {   
    alert (ARGUMENTS.CALLEE.I);   
  }). i = i;   
 }   

3. Add a layer closure, I pass the function parameter form to the inner layer function

function Init3 () {   
 var pary = document.getElementsByTagName ("P");   
 for (var i=0; i<pary.length; i++) {   
  (function (ARG) {     
    Pary[i].onclick = function () {     
     alert (arg);   
    };   
  }) (i);//Call time parameter   
 }   
}   

4. Add a layer closure, I pass to the memory function as local variable

function Init4 () {   
 var pary = document.getElementsByTagName ("P");   
 for (var i=0; i<pary.length; i++) {    
  (function () {   
   var temp = i;//called local variable   
   pary[i].onclick = function () {
   alert (temp);    
   }   
  ) ();   
 }   
}   

5, add a layer closure, return a function as a response event (note the subtle difference with 3)

function Init5 () {   
 var pary = document.getElementsByTagName ("P");   
 For (Var i=0. i<pary.length; i++) {    
  Pary[i].onclick = function (arg) {   
    return function () {//Returns a function   
    Aler T (ARG);   
   }   
  } (i);   
 }   
  

6, with function implementation, in fact, each generated a function instance will produce a closure

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

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

function Init7 () {   
  var pary = document.getElementsByTagName ("P");   
  for (var i=0; i<pary.length; i++) {   
     Pary[i].onclick = Function (' Alert (' +i+ ')  
  }   
}}  

The above is a small series for everyone to talk about JavaScript for the loop closure of the entire content, I hope that we support cloud-Habitat Community ~

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.