
30 ActionScript 2.0 to 3.0
Default values
The following example shows the default values for variables of different types:
var dog:int; // defaults to 0
var dog:uint; // defaults to 0
var dog:Boolean;// defaults to false
var dog:Number; // defaults to NaN
var dog:Object; // defaults to undefined
var cat; // type defaults to Object, so default value is undefined
For all other classes (such as String, XML, MovieClip, Sprite, or any user-defined class), null
is the default value for uninitialized variables of that type.
var dog:String; // defaults to null
var dog:UserDefinedClass; // defaults to null
var dog:MovieClip; // defaults to null
With the exception of Object, the types with a non-null default value are the types which can
never hold
null as a value. They get what a typecast of null to that type would result in.
To check for a possibly uninitialized variable, compare against the defaults described in
“Default values” on page 30. Alternatively, assign an initial, impossible value to your variable’s
declaration which you can then test against; for example:
var length:int = -1;
if (someTest())
length = 2;
if (someOtherTest())
length = 3;
if (length == -1)
length = lastResort();
For instances of classes, use the following pattern:
if (o) // ActionScript 3.0
if (o != null) // ActionScript 2.0
and
if (!o) // ActionScript 3.0
if (o == null) // ActionScript 2.0
Because a Boolean can only be true or false, and never undefined, write the following when
checking if a Boolean is true:
if (b) // ActionScript 3.0
if (b == true) // ActionScript 2.0
if (b != false) // ActionScript 2.0
Comentários a estes Manuais