On 9/7/06, Ryan Gahl <[EMAIL PROTECTED]> wrote:
Wow, that's in sharp contrast to how I thought it worked, and in fact does work in most languages. For instance in C# or Java, the {} of the for loop would represent a new scope per iteration, so the declared variables would only apply to that iteration's scope, and get garbage collected with each iteration, meaning a new address in memory would be used for the next one. Therefore, it would be possible to force a value copy of a referenced object, possibly using a boxing method if required (like .toString()) or of course constructing a new object using the value of the referenced one.

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()")


The big difference is that _javascript_ only creates a new execution context for each function, not each block
(different from C++). I found this reference:

http://www.jibbering.com/faq/faq_notes/closures.html

The explanation I found was that _javascript_ "does not have block scoping".

As a very short example, this code:

if (true) {
 
var i=1;
}
alert(i);

will show 1, not undefined. (Example from http://overstimulate.com/articles/2006/02/06/_javascript_-scoping.html )

Hope this helps!


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

Reply via email to