for (var i:int = 0; i < 10; i++)
{
  if (i == 5) break;
}
trace(i);

Mmm, have you actually tested the example? Because it does trace 5, since,
as it was explained earlier in this thread, there is no block level scoping
in AS 3.0. In fact, and this was mentioned too, all var declarations are
"moved up" to be executed as the first actions run in a function's code (I
believe that was called hoisting, but I might be wrong).

Cheers
Juan Pablo Califano

2008/3/27, Steven Sacks <[EMAIL PROTECTED]>:
>
> function doSomething
> > {
> >   var i:int;
> >   for(i=0;i++;i<10)
> >   {
> >   }
> > }
> >
> > Is functionally identical to this:
> >
> > function doSomething
> > {
> >   for(var i:int =0;i++;i<10)
> >   {
> >   }
> > }
>
> Wrong.  It's not.
>
> In the latter example, i is not available after the loop.  In the first
> example, it is.
>
> var i:int;
> for (i = 0; i < 10; i++)
> {
>   if (i == 5) break;
> }
> trace(i);
> -- 5
>
> There are a multitude of uses for this, and I do it all the
> time.  Additionally, I read somewhere many moons ago (back in my FLASM days)
> that declaring variables outside a for loop is less bytecode and uses less
> memory.  I don't believe that applies to the counter declaration, but I do
> know it applies to the comparison as well as vars declared inside the for
> loop.  However, this level of optimization is only useful in a practical way
> on mobile and some games.
>
> _______________________________________________
> Flashcoders mailing list
> [email protected]
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to