Ian, help is on the way, the ECMAScript 4th edition draft specification
contains a new keyword, "let", that can be used in place of "var" to
provide block-level scoping. Details for the curious:

http://wiki.ecmascript.org/doku.php?id=proposals:block_expressions

Francis Cheng | Senior Technical Writer | Adobe Systems, Inc.
http://blogs.adobe.com/fcheng

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Ian
Thomas
Sent: Thursday, March 27, 2008 1:23 PM
To: Flash Coders List
Subject: Re: [Flashcoders] Variable scope within for loops:
reusingiteratorvariables

AFAIK, in AS2 the Flash IDE didn't respect block level scoping, but
MTASC did, which led to some confusion. That leads some people to
think that AS2 as a language has block level scoping.

AS3 definitely doesn't respect block scopes, and I curse every time I
trip over that 'variable declared twice' issue. I wish it did.

Ian

On Thu, Mar 27, 2008 at 8:09 PM, Juan Pablo Califano
<[EMAIL PROTECTED]> wrote:
> 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
>
_______________________________________________
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