closure problem

Discussion in 'Javascript' started by jman, Mar 6, 2008.

  1. jman

    jman Guest

    for ( var i = 0; i < div.firstChild.childNodes.length; ++i )
    {
    var marker = new Object();
    marker.iii = i;

    sys.addListener( marker, "click", function()
    {
    alert(marker.iii);
    });
    }

    looks like when clicked - marker object has the value of the last
    object thru the loop.

    i'd like it to display the appropriate iii value.
     
    jman, Mar 6, 2008
    #1
    1. Advertising

  2. On Mar 6, 8:54 am, jman <> wrote:
    > for ( var i = 0; i < div.firstChild.childNodes.length; ++i )
    > {
    > var marker = new Object();
    > marker.iii = i;
    >
    > sys.addListener( marker, "click", function()
    > {
    > alert(marker.iii);
    > });
    > }
    >
    > looks like when clicked - marker object has the value of the last
    > object thru the loop.
    >
    > i'd like it to display the appropriate iii value.


    JavaScript has function level scoping as opposed to block level (like
    other languages). Your marker object is visible across iterations and
    the closure will always reference the last instance of the marker
    object. I believe you could stop this from happening using a self-
    executing anonymous function, like so:

    for ( var i = 0; i < div.firstChild.childNodes.length; ++i )
    {
    (function() {
    var marker = new Object();
    marker.iii = i;

    sys.addListener( marker, "click", function()
    {
    alert(marker.iii);
    });
    })(); /* These brackets are necessary to execute the anonymous
    function */
    }

    This should scope the marker inside the anonymous function and the
    closure should behave the way you want.
     
    Nick Fletcher, Mar 6, 2008
    #2
    1. Advertising

  3. Nick Fletcher wrote/zu Deiner Priorität-Alpha-1-Nachricht von Sternzeit
    06.03.2008 18:16:
    > On Mar 6, 8:54 am, jman <> wrote:
    >> for ( var i = 0; i < div.firstChild.childNodes.length; ++i )
    >> {
    >> var marker = new Object();
    >> marker.iii = i;
    >>
    >> sys.addListener( marker, "click", function()
    >> {
    >> alert(marker.iii);
    >> });
    >> }
    >>
    >> looks like when clicked - marker object has the value of the last
    >> object thru the loop.
    >>
    >> i'd like it to display the appropriate iii value.

    >
    > JavaScript has function level scoping as opposed to block level (like
    > other languages).


    JFYI: Since version 1.7 it also has block scoping, but you need to declare
    that version as it introduces new keywords.

    http://developer.mozilla.org/en/docs/New_in_JavaScript_1.7#Block_scope_with_let

    > Your marker object is visible across iterations and
    > the closure will always reference the last instance of the marker
    > object. I believe you could stop this from happening using a self-
    > executing anonymous function, like so:
    >
    > for ( var i = 0; i < div.firstChild.childNodes.length; ++i )
    > {
    > (function() {
    > var marker = new Object();
    > marker.iii = i;
    >
    > sys.addListener( marker, "click", function()
    > {
    > alert(marker.iii);
    > });
    > })(); /* These brackets are necessary to execute the anonymous
    > function */
    > }
    >
    > This should scope the marker inside the anonymous function and the
    > closure should behave the way you want.


    You create an additional closure of `i' needlessly. It is overkill
    and the base code is still very inefficient and error-prone.

    Consider this instead:

    for (var i = 0, len = div.firstChild.childNodes.length; i < len; ++i)
    {
    var marker = new Object();
    marker.iii = i;

    sys.addListener(
    marker, "click",
    (function(me) {
    return function() {
    window.alert(me.iii);
    };
    })(marker)
    );
    }


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Mar 7, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John J. Lee
    Replies:
    4
    Views:
    379
    John J. Lee
    Nov 8, 2003
  2. Replies:
    4
    Views:
    252
  3. Replies:
    7
    Views:
    128
    Paul Stickney
    Jan 12, 2008
  4. Julian Mehnle
    Replies:
    0
    Views:
    253
    Julian Mehnle
    Jul 17, 2003
  5. Martin Honnen

    Problem with a closure and the return value

    Martin Honnen, Dec 23, 2004, in forum: Javascript
    Replies:
    2
    Views:
    105
    Börni
    Dec 23, 2004
Loading...

Share This Page