Undefined vs. undeclared
Undefined and undeclared variables are two different things. This is probably obvious to some, but in ActionScript, it isn't always so obvious. An undefined variable is a variable that has a value of undefined. An undeclared variable has never been set to a value or established with var. In ActionScript right now, undefined and undeclared variables have pretty much the same effect.
var a;
if (a == undefined) {
trace("a is undefined");
}
is the same as
a = undefined;
if (a == undefined) {
trace("a is undefined");
}
and also the same as
if (a == undefined) {
.
trace("a is undefined");
}
The if statement evaluates to true, i.e. the value of a is undefined, in all three cases. In reality though, a is undefined only in the first and second examples while it is undeclared in the last one. In some other languages, the last example is not legal. It will result in a compiler error because we are trying to get the value of the variable before it is declared. Descpite the relative leniency of ActionScript, this is a good thing to keep in mind. Sometimes, when we think we are dealing with undefined variables, they are really undeclared.





posted
by Vera (