radiofreekruzr

Untitled

Jan 19th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2.  
  3. <html>
  4. <head>
  5. <title></title>
  6. </head>
  7. <body>
  8.     <h3>(Press F12 to display console.) Click one of the links below.
  9.     </h3>
  10.     <script type="text/javascript">
  11.    
  12.         // Each "a" element gets the correct text,
  13.         // i.e. "Link 0", "Link 1" and so on. But whichever
  14.         // link you click, it always alerts the number "5".
  15.         // Why? (The reason is explained below.)
  16.                
  17.         // Declare a outer function.  Note that anonymous
  18.         // inner functions are being registered as event
  19.         // handlers for "link" (see line #29)
  20.         function addLinksWithoutClosure() {
  21.             for ( var i = 0, link; i < 5; i++) {
  22.                
  23.                 // Create an anchor and add it to the <body> element
  24.                 link = document.createElement("a");
  25.                 link.innerHTML = "LinkWithoutClosure " + i + "<br/>";
  26.  
  27.                 // function is defined - in other words, a function object is
  28.                 // created.
  29.                 link.onclick = function() {
  30.                     alert(i); console.log(i);
  31.                 };
  32.                 document.body.appendChild(link);
  33.             }
  34.             // When addLinksWithoutClosure() outer
  35.             // function's execution is done, the value
  36.             // of "i" is set to 5.
  37.         }
  38.        
  39.         // Invoke the outer function.  Now the inner function
  40.         // is defined with its function scope - the variable i,
  41.         // which is set to 5.  When link is clicked, the inner
  42.         // function gets executed with value of 5.
  43.         addLinksWithoutClosure();
  44.  
  45.         //
  46.         // Use Closure to the solve the problem above
  47.         //
  48.  
  49.         function addLinksWithClosure() {
  50.             for ( var i = 0, link; i < 5; i++) {
  51.                 link = document.createElement("a");
  52.                 link.innerHTML = "LinkWithClosure " + i + "<br/>";
  53.  
  54.                 // Note that self-invoking function is used here.
  55.                 // The correct value of "i" is passed to the
  56.                 // self-invoking "function(value){..}", which
  57.                 // provides the correct value to its own
  58.                 // internal function.
  59.                 link.onclick = (function(value) {
  60.                     return function() {
  61.                        alert(value); console.log(i);
  62.                     }
  63.                 })(i);
  64.                 document.body.appendChild(link);
  65.             }
  66.         }
  67.         addLinksWithClosure();
  68.     </script>
  69. </body>
  70. </html>
Advertisement
Add Comment
Please, Sign In to add comment