Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > closure problem

Reply
Thread Tools

closure problem

 
 
jman
Guest
Posts: n/a
 
      03-06-2008
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.
 
Reply With Quote
 
 
 
 
Nick Fletcher
Guest
Posts: n/a
 
      03-06-2008
On Mar 6, 8:54 am, jman <erjdri...@gmail.com> 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.
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      03-07-2008


Nick Fletcher wrote/zu Deiner Priorität-Alpha-1-Nachricht von Sternzeit
06.03.2008 18:16:
> On Mar 6, 8:54 am, jman <erjdri...@gmail.com> 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...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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with closure/block levilista@gmail.com Ruby 7 01-12-2008 06:57 AM
Closure/binding problem or misunderstanding Bob.Sidebotham@gmail.com Python 4 11-09-2007 03:13 PM
Problem with a closure and the return value Börni Javascript 2 12-23-2004 06:10 PM
problem with closure arguments and *args in mock object John J. Lee Python 4 11-08-2003 12:28 PM
Perl hangs when returning lvalue closure from another lvalue closure Julian Mehnle Perl Misc 0 07-17-2003 03:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57