JS Closure Demo

Source: Internet
Author: User

A netizen asked a question, such as the next HTML, why each output is 5

  1. <html >
  2. <head>
  3. <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 " />
  4. <title> Closure demo </title>
  5. <style type="Text/css">
  6. </style>
  7. <script type="Text/javascript">
  8. function init () {
  9. var pary = document.getelementsbytagname ("P");
  10. for (var i=0; I<pary.length; i++) {
  11. Pary[i]. onclick = function () {
  12. alert (i);
  13. }
  14. }
  15. }
  16. </Script>
  17. </head>
  18. <body onload="init ();" >
  19. <P> Product one </P>
  20. <P> Product one </P>
  21. <P> Product one </P>
  22. <P> Product one </P>
  23. <P> Product one </P>
  24. </body>
  25. </html>

There are two ways to solve this 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. }

Add 3 more kinds

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 parameters
  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 = i; //local variables at call time
  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. }

There's another way.

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

from:http://zhouyrt.javaeye.com/blog/250073

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.