A netizen asked a question, such as the next HTML, why click All the paragraph p output is 5, instead of alert out corresponding 0,1,2,3,4.
- <! DOCTYPE html>
- <meta charset= "Utf-8"/>
- <title> Closure Demo </title>
- <style type= "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>
- <body onload= "Init ();" >
- <p> Products 0</p>
- <p> Products 1</p>
- <p> Products 2</p>
- <p> Products 3</p>
- <p> Products 4</p>
- </body>
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)
- 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
- 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
- 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
- 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)
- 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
- 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
- function Init7 () {
- var pary = document.getElementsByTagName ("P");
- for (var i=0; i<pary.length; i++) {
- Pary[i].onclick = Function (' Alert (' +i+ ') ');
- }
- }
"Responsible editor: Chen Yu new TEL: (010) 68476606"