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),
is the default value for uninitialized variables of that type.
var dog:String;
var dog:UserDefinedClass;
var dog:MovieClip;
With the exception of Object, the types with a non-null default value are the types which can
never hold
as a value. They get what a typecast of
null
To check for a possibly uninitialized variable, compare against the defaults described in
"Default values" on page
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
30
ActionScript 2.0 to 3.0
// defaults to null
// defaults to null
// defaults to null
30. Alternatively, assign an initial, impossible value to your variable's
to that type would result in.
null
null
Need help?
Do you have a question about the FLEX 2-MIGRATING APPLICATIONS TO FLEX 2 and is the answer not in the manual?
Questions and answers