That is scary! ;)
--- In [email protected], "Michael Prescott"
<[EMAIL PROTECTED]> wrote:
>
> *Why* it's different I can't tell you, but if you're being regularly
> surprised, it sounds like you might benefit from skimming through
the
> language reference. This behavior is described on the
> Variables<http://livedocs.adobe.com/flex/3/html/help.html?
content=03_Language_and_Syntax_07.html#118946>page
> of help resource center, under 'Understanding variable scope'.
>
> From there (this may scare you even more!):
>
> *ActionScript variables, unlike variables in C++ and Java, do not
have
> block-level scope. ... An interesting implication of the lack of
block-level
> scope is that you can read or write to a variable before it is
declared, as
> long as it is declared before the function ends. This is because of
a
> technique called hoisting, which means that the compiler moves all
variable
> declarations to the top of the function.*
>
> On Thu, Dec 4, 2008 at 5:31 PM, toofah_gm <[EMAIL PROTECTED]> wrote:
>
> > I came across another scoping issue today that scared me. I sure
wish
> > that the scoping in the ActionScript language worked the same way
C++,
> > C#, Java, and others work!
> >
> > Here's the code:
> >
> > for each (var o:Object in myArray)
> > {
> > var myProblemObject:ProblemObject;
> > if (some condition)
> > {
> > myProblemObject = new ProblemObject();
> > ... more code here ...
> > }
> > ... still more code here...
> >
> > if (myProblemObject)
> > {
> > ... do some stuff ...
> > }
> > }
> >
> > In this code myProblemObject is NULL when things start
out...great!
> > The problem comes after an instance of myProblemObject gets
created.
> > Every time through the loop after this myProblemObject is no
longer
> > NULL. This is not consistent with any other language that I have
> > worked with and is therefore not obvious to the developer working
with
> > the code.
> >
> > Why is myProblemObject not reset each time through the loop? It is
a
> > variable declaration.
> >
> > Gary
> >
> > --- In [email protected] <flexcoders%40yahoogroups.com>,
> > "toofah_gm" <garym@> wrote:
> > >
> > > It seems to me and my co-workers that the Flex compiler is
broken
> > when
> > > it comes to local variable scoping within methods.
> > >
> > > For example:
> > >
> > > for (var i:int=0; i<count; i++)
> > > {
> > > // do something
> > > }
> > >
> > > for (var i:int=0; i<count; i++)
> > > {
> > > // do something else
> > > }
> > >
> > > This gives a compiler warning stating that 'i' is already
defined.
> > > But in every other language that I have used, this is completely
> > > valid. Yes 'i' was defined above, but 'i' should only be scoped
> > > within the 'for' loop and should be invalid outside of it.
> > >
> > >
> > >
> > > Another example:
> > >
> > > if (x)
> > > {
> > > var myArray:Array = new Array();
> > > // do more stuff
> > > }
> > >
> > > myArray.push("some data");
> > >
> > > This one compiles, when I believe that it shouldn't. myArray
should
> > > only be defined within the 'if' statement. If you don't go into
the
> > > 'if' statement you have a problem here.
> > >
> > >
> > > Does anyone understand why the Flex compiler allows this? Is
this
> > > just a BUG with the compiler?
> > >
> > > Anyway, this is just driving me a little crazy. ;)
> > >
> > > Gary
> > >
> >
> >
> >
>