{But adding those {} to make a compound statement does NOT work in
int i = 0;
}
{
int i = 1; //Legal in Java
}
ActionScript 3 because the AS3 compiler takes all the local variables
(regardless of depth) and declares that right at the beginning of the
function. For example, this is legal in AS3:
trace(i); // Legal in AS3, default value of int (zero) is outputIt appears we're accessing a variable before it is declared. But since the compiler moved our declaration to the beginning of the function, there is no error. The assignment is not moved, the the value of i at that point is zero. This behavior is similar to the way Flash deals with variables
var i:int = 1;
trace(i);
declared later on the timeline, you can reference them before earlier
than the declaration because the compiler moves that declaration to
the beginning.
Yea. Real annoying if you have two for loops in the same function that declare i:int in the loop headers. AS3 will warning of duplicate variable.
ReplyDelete