JavaScript Closure Demo Code Summary _javascript Tips

Source: Internet
Author: User
Tags closure
<! DOCTYPE html> <ptml> <pead> <meta charset= "utf-8"/> <title> closure demo </title> <style t Ype= "Text/css" > P {background:gold;} </style> <script type= "Text/javascript" > Function init () {var pary = document.getElementsByTagName ("P"); for (var i=0; i<pary.length; i++) {Pary[i].onclick = function () {alert (i); }} </script> </pead> <body onload= "init ();" > <p> Products 0</p> <p> products 1</p> <p> products 2</p> <p> products 3</p> <p> products 4< ;/p> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

The above scenes are often encountered by beginners. Gets the collection of HTML elements to iterate over the element add event. Gets the corresponding index (event handler) in the incident response function. But each fetch is the index of the last loop.
The reason is that beginners do not understand the closure characteristics of JavaScript. Via Element.onclick=function () {alert (i);} method to add a click event to the element. Response functions function () {alert (i);} I is not the corresponding I (such as 0,1,2,3,4) in each loop, but the value of the last I after the loop is 5. Or the loop-time response function is not able to save the corresponding value I, but the last i++ value of 5.

Understand the cause and find out a lot of solutions (purely interest). First two kinds of thinking
1. Save the variable i on each paragraph object (p)
Copy Code code as follows:

function Init1 () {
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
Copy Code code as follows:

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;
}
}

And then I thought of three more.
3. Add a layer closure, I pass the function parameter form to the inner layer function
Copy Code code as follows:

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 of closure, I in the form of local variables passed to the inner layer function
Copy Code code as follows:

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

5, add a layer closure, return a function as a response event (note the subtle difference with 3)
Copy Code code as follows:

function Init5 () {
var pary = document.getElementsByTagName ("P");
for (var i=0; i<pary.length; i++) {
Pary[i].onclick = function (ARG) {
return function () {//returns a
Alert (ARG);
}
} (i);
}
}

And then found two more
6, with function implementation, in fact, each generated a function instance will produce a closure
Copy Code code as follows:

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

7, with function implementation, pay attention to the difference with 6
Copy Code code as follows:

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

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.