This is variable hoisting in action:

http://docs.huihoo.com/web/js/es4/core/definitions.html#hoist

I can't really find a clearer explanation, but the gist of it is that
variable definitions are sometimes moved to the parent block, and
sometimes this results in the sort of weird behavior you're seeing.

That's the how, but I don't really know the why--anyone more savvy about
hoisting care to enlighten the list?

-- 
Maciek Sakrejda
Truviso, Inc.
http://www.truviso.com

-----Original Message-----
From: toofah_gm <[EMAIL PROTECTED]>
Reply-To: [email protected]
To: [email protected]
Subject: [flexcoders] Method variable scoping problem with Flex compiler
Date: Fri, 15 Aug 2008 19:43:09 -0000

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




 


Reply via email to