Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 60

Programming actionscript 3.0
Table of Contents

Advertisement

The
operator examines the proper inheritance hierarchy and can be used to check not only
is
whether an object is an instance of a particular class, but also whether an object is an instance
of a class that implements a particular interface. The following example creates an instance of
the Sprite class named
instance of the Sprite and DisplayObject classes, and whether it implements the
IEventDispatcher interface:
var mySprite:Sprite = new Sprite();
trace(mySprite is Sprite);
trace(mySprite is DisplayObject);
trace(mySprite is IEventDispatcher);
The
operator checks the inheritance hierarchy and properly reports that
is
compatible with the Sprite and DisplayObject classes (the Sprite class is a subclass of the
DisplayObject class). The
that implement the IEventDispatcher interface. Because the Sprite class inherits from the
EventDispatcher class, which implements the IEventDispatcher interface, the
correctly reports that
The following example shows the same tests from the previous example, but with
instead of the
operator. The
is
instance of Sprite or DisplayObject, but it returns
implements the IEventDispatcher interface.
trace(mySprite instanceof Sprite);
trace(mySprite instanceof DisplayObject);
trace(mySprite instanceof IEventDispatcher); // false
The as operator
The
operator, which is new in ActionScript 3.0, also allows you to check whether an
as
expression is a member of a given data type. Unlike the
does not return a Boolean value. Rather, the
instead of
, and
true
the
operator instead of the
as
instance is a member of the DisplayObject, IEventDispatcher, and Number data types.
var mySprite:Sprite = new Sprite();
trace(mySprite as Sprite); // [object Sprite]
trace(mySprite as DisplayObject); // [object Sprite]
trace(mySprite as IEventDispatcher); // [object Sprite]
trace(mySprite as Number);
When you use the
as
use an expression other than a data type as the operand on the right will result in an error.
60
ActionScript Language and Syntax
and uses the
mySprite
operator also checks whether
is
implements the same interface.
mySprite
instanceof
instead of
null
false
operator in the simple case of checking whether a Sprite
is
// null
operator, the operand on the right must be a data type. An attempt to
operator to test whether
is
// true
// true
// true
mySprite
operator correctly identifies that
when used to test whether
false
// true
// true
operator, however, the
is
operator returns the value of the expression
as
. The following example shows the results of using
is an
mySprite
is
mySprite
inherits from any classes
operator
is
instanceof
is an
mySprite
mySprite
operator
as

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents