A while back I read a post about variable hoisting in Actionscript , and remember thinking how it was weird how that was the first time I'd ever heard about that, and how I'd probably never need to know that if I hadn't until that point anyway. So today I get burned by it. When you declare a variable anywhere within a function, Actionscript 'hoists' that declaration to the top of the function. To quote the original article:

Any variable defined within a function scope, in any structure, at any location, is automatically moved at compile-time to the top of the scope. And it’s accessible anywhere in any of those scopes. In most languages, this would be a compile error.

This is what got me today. In this sample, mainUI and someArray are other properties of the object, but vo is not:

Actionscript:
  1. override public function execute():void {
  2.     var container:Canvas = mainUI.getContainer(vo.dataProvider);
  3.     var dp:ArrayCollection = container.dataProvider;
  4.  
  5.     for each(var vo:ItemVO in someArray){
  6.         doStuff(vo);
  7.    
  8.     }
  9. }

As you can see, in the first line of code I try to call vo.dataProvider, but it should not be defined yet. However, since I declare the var 'vo' in the for each loop, the compiler lifted the declaration to the top of the function, which means that as far as the compiler cares, the vo variable existed before my first line of code.