Scoping is pretty weird in AS3 for those coming from C/C++ and Java. There are two cases in particular you should know about.

The first you’ll probably see pretty quickly upon starting your AS3 programming:

function printTwice(a:Array): void
{
	for each (var cur:String in a)
	{
		trace(cur);
	}
	for each (var cur:String in a)
	{
		trace(cur);
	}
}

You get this compiler warning twice:

Warning: Duplicate variable definition.
 
			for each (var cur:String in a)
			              ^

This betrays what’s going on behind the scenes: there is no loop scope! All local variables are scoped to the function. So there’s a clash here, but it’s OK because the compiler will just pretend that you didn’t declare the second one and instead just re-use the first. This is dangerous, hence the compiler warning.

Recently though, I discovered this evil little variable clash:

function printLength(a:Array): void
{
	var a:Array;
	trace(a.length);
}

Firstly, there are no compiler errors or warnings this time around. There should be. Secondly, say I call printLength([2,4,6]). What do you figure gets printed? The answer is nothing. The a declared in the function overrides the a declared in the arguments list. There is, in fact, no way to reference the argument by its name (a), only by arguments[0]. So the trace statement is accessing a null reference and you will get a crash. Just like I did. I hope, having read this article, you will be extra vigilant with this. And if you’re not, I at least hope you can debug it faster.