The following example shows the results of casting built-in object types. As the first line in the
with(results)
returns
. As the last two lines show, casting to null or undefined returns
null
var mc:MovieClip;
var arr:Array;
var bool:Boolean;
var num3:Number;
var obj:Object;
var str:String;
_root.createTextField("results",2,100,100,300,300);
with(results){
text = "type MovieClip : "+(typeof MovieClip(str));
text += "\ntype object : "+(typeof Object(str));
text += "\ntype Array : "+(typeof Array(num3));
text += "\ntype Boolean : "+(typeof Boolean(mc));
text += "\ntype String : "+(typeof String(mc));
text += "\ntype Number : "+(typeof Number(obj));
text += "\ntype Function : "+(typeof Function(mc));
text += "\ntype null : "+(typeof null(arr));
text += "\ntype undefined : "+(typeof undefined(obj));
}
//Results in Output panel
type MovieClip : null
type object : object
type Array : object
type Boolean : boolean
type String : string
type Number : number
type Function : object
type null : undefined
type undefined : undefined
You can't override primitive data types such as Boolean, Date, and Number with a cast operator of
the same name.
About variables
A variable is a container that holds information. The container itself is always the same, but the
contents can change. By changing the value of a variable as the SWF file plays, you can record and
save information about what the user has done, record values that change as the SWF file plays, or
evaluate whether a condition is true or false.
It's a good idea always to assign a variable a known value the first time you define the variable.
This is known as initializing a variable and is often done in the first frame of the SWF file.
Initializing a variable helps you track and compare the variable's value as the SWF file plays.
Variables can hold any type of data (see
variable contains affects how the variable's value changes when it is assigned in a script.
Typical types of information that you can store in a variable include a URL, a user's name, the
result of a mathematical operation, the number of times an event occurred, or whether a button
has been clicked. Each SWF file and movie clip instance has a set of variables, with each variable
having a value independent of variables in other SWF files or movie clips.
40
Chapter 2: ActionScript Basics
block shows, an illegal cast—in this case, casting a string to a movie clip—
"About data types" on page
.
undefined
// returns null
// returns object
// returns object
// returns boolean
// returns string
// returns number
// returns object
// returns undefined
// returns undefined
34). The type of data a
Need help?
Do you have a question about the FLASH MX 2004 - ACTIONSCRIPT and is the answer not in the manual?