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)
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
