MACROMEDIA FLEX-FLEX ACTIONSCRIPT LANGUAGE Reference page 26

Actionscript language reference
Table of Contents

Advertisement

The syntax for casting is
of
is
. Casting is essentially a function call, and the function call returns
item
type
fails at runtime. If the cast succeeds, the function call returns the original object. However, the
compiler cannot determine whether a cast will fail at runtime and won't generate compile-time
errors in those cases. The following code shows an example:
function bark(myAnimal:Animal) {
var foo:Dog = Dog(myAnimal);
foo.bark();
}
var curAnimal:Animal = new Dog();
bark(curAnimal); // will work
curAnimal = new Cat();
bark(curAnimal); // won't work
In this situation, you asserted to the compiler that
compiler assumes that
that the cast will fail (that is, that you tried to cast a Cat object to an Animal type), so no compile-
time error occurs. If you include a check in your script to make sure that the cast succeeds, you
can find casting errors at runtime.
import Dog;
function bark(myAnimal:Animal) {
var foo:Dog = Dog(myAnimal);
if (foo) {
foo.bark();
}
}
You can cast an expression to an interface. If the expression is an object that implements the
interface or has a base class that implements the interface, the cast succeeds. If not, the cast fails.
Casting to null or undefined returns
You can't override primitive data types that have a corresponding global conversion function with
a cast operator of the same name. This is because the global conversion functions have precedence
over the cast operators. For example, you can't cast to Array because the
function takes precedence over the cast operator. For more information on data conversion
functions, see
Array()
Determining an item's data type
While testing and debugging your programs, you might discover problems that seem to be related
to the data types of different items. In these cases, you may want to determine an item's data type.
You can use either the
Use the
typeof
class to which an instance belongs. Use the
specified data type or not;
26
Chapter 1: ActionScript Basics
, where you want the compiler to behave as if the data type
type(item)
; is a legal statement. However, the compiler doesn't know
temp.bark()
undefined
,
,
Boolean()
Number()
operator or the
typeof
operator to get the data types;
instanceof
is a Dog object, and, therefore, the
foo
.
,
, and
Object()
operator.
instanceof
does not return information about which
typeof
operator to determine if an object is of a
instanceof
returns a Boolean value.
null
conversion
Array()
.
String()
if the cast

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLEX-FLEX ACTIONSCRIPT LANGUAGE and is the answer not in the manual?

Subscribe to Our Youtube Channel

This manual is also suitable for:

Flex

Table of Contents