Obviously, I've been babied now for a while with the $A().each() iterators of prototype, so haven't had to face this problem for a little while in the _javascript_ domain.
Ok, so how about these? I'm moving the variable declaration around the lexical pads here to see where the contexts are actually being recreated, so to speak... (still academic discussion for the sole purpose of getting smarter :-))
for the "new_i = i" assignment, I'd try any of the three options to try to achieve the desired result, but won't write all the permutations here. (the options being "new_i = i", "new_i = new Number(i)" and "new_i = i.toString()")
var new_i;
for (var i=1; i<=5;i++){
new_i = i;
Event.observe($('star'+new_i), 'click',
function(e){objRating.setRating(new_i)});
}
or... (in this one, the regular i in the first argument of the Event.observe function is fine, and actually I'm assuming that part has worked fine all along) -- now, this one should really work because the var new_i is being declared in the closure, which should be a brand new scope per iteration of the loop, so one of the 3 assignment options cited above should be able to coerce the value copy we're looking for.
for (var i=1; i<=5;i++){
Event.observe($('star'+i), 'click',
function(e){var new_i = i; objRating.setRating(new_i)});
}
On 9/7/06,
Matt Jones <[EMAIL PROTECTED]> wrote:
On 9/7/06, Ryan Gahl < [EMAIL PROTECTED]> wrote:Interesting. I assumed new_i = i would be making a value copy to the new reference, which should be scoped each time through the loop... hmm.
So I wonder if this is what I needed to do... (yes agreed it's academic, but fun to carry on)
for (var i=1; i<=5;i++){var new_i = new Number(i);
Event.observe($('star'+new_i), 'click',
function(e){objRating.setRating (new_i)});
}...or even...
for (var i=1; i<=5;i++){var new_i = i.toString();
Event.observe($('star'+new_i), 'click',
function(e){objRating.setRating(new_i)});
}Now I would really assume either of those should work to force a value copy to the new variable with each iteration. If not, I give up :-)The two ways above both end up referencing the same execution context. In basic terms,
new_i is the same place on the "stack" each time, and the created functions all point to it.
(Using quotes because the implementation is not important, just the idea.)
That's why the "function returning a closure" bit mentioned previously works; each time
that function is invoked, an new "stack frame"/execution context is created.
Of course, in Lisp one rarely has these problems, as the loop is typically represented as
a tail recursive procedure... I think that would really throw people off in JS.
--
Matt Jones
[EMAIL PROTECTED]
President/Technical Director, Acme Art Company (acmeartco.org)
--
Ryan Gahl
Application Development Consultant
Athena Group, Inc.
Inquire: 1-888-919-8700 x2903
Blog: http://www.someElement.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs
-~----------~----~----~----~------~----~------~--~---
