Is the second better than the first?
FIRST:
var count:int=myArray.length;
for(var i:uint=0;i<count;i++)
{
var str:String=myArray[i].label;
var somethingElse:Class=...;
var andAnotherThing:MyInstance=new MyInstance(somethingElse);
...
}
SECOND:
var count:int=myArray.length;
var str:String;
var somethingElse:Class;
var andAnotherThing:MyInstance;
for(var i:uint=0;i<count;i++)
{
str=myArray[i].label;
somethingElse=...;
andAnotherThing=new MyInstance(somethingElse);
...
}
Thank you.
In Actionscript and Javascript, variables are scoped to the function, not the block. It’s called variable hoisting.
ActionScript 3.0 Variables
So effectively your code will behave like this regardless of where you declare your variables within the function:
Nevertheless, I still prefer to declare my variables within the blocks that use them primarily for maintenance reasons and general clarity.