SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 30 of 30
  1. #26
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,751
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dek
    Since when?
    HTML Code:
    <html>
    <head>
    <script type="text/javascript">
    function addEventHandlers()
    {
      div_list = document.getElementsByTagName('div');
      for ( var i = 0 ; i < div_list.length ; i++ )
      {
         div_list[i].onclick = function() { alert(i); };
      }
    }
    </script>
    </head>
    <body>
    <div>foobar</div>
    <div>barfoo</div>
    <script type="text/javascript">
    addEventHandlers();
    </script>
    </body>
    </html>

  2. #27
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ... and off the top of my head, each of those divs will alert you with the latest value of i (2) when you click on them. That's a closure. Still don't see your point...
    Only dead fish go with the flow

  3. #28
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,751
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dek
    ... and off the top of my head, each of those divs will alert you with the latest value of i (2) when you click on them. That's a closure. Still don't see your point...
    How is that a closure? If it was true closure than the first would produce 0 (the initial loop counter value) and the second would produce 1 as:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function addEventHandlers()
    {
      div_list = document.getElementsByTagName('div');
      for ( var i = 0 ; i < div_list.length ; i++ )
      {
        addOnClick(div_list[i],i);
      }
    }
    
    function addOnClick(element,counter)
    {
      div_list[i].onclick = function() { alert(i); };
    }
    </script>
    </head>
    <body>
    <div>foobar</div>
    <div>barfoo</div>
    <script type="text/javascript">
    addEventHandlers();
    </script>
    </body>
    </html>
    does. Maybe I'm not expressing the correct problem but.. I consider that an issue.

  4. #29
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris_fuel
    I thought you said that code was broke in IE6, and yes, I meant the memory leak.
    No, I never said it was broken, it was a testcase to show that IE6 leaks badly with closures and that the leak is gone in IE 7 beta 2

  5. #30
    SitePoint Addict dek's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chris. I think you meant to type the following:

    Code:
    function addOnClick(element,counter)
    {
      element.onclick = function() { alert(counter); };
    }
    Yes, that is another closure. It will achieve the result you want, because this time you're passing one instance of i into a different scope.

    The mechanism, however is exactly the same. If you change it to:

    Code:
    function addOnClick(element,counter)
    {
      element.onclick = function() { alert(counter); };
      counter = 200;
    }
    You should see what I mean.
    Only dead fish go with the flow


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •