The is operator, which is new for ActionScript 3.0, allows you to test whether a variable or expression is a member of a given data type. In previous versions of ActionScript, the instanceof operator provided this functionality, but in ActionScript 3.0 the instanceof operator should not be used to test for data type membership. The is operator should be used instead of the instanceof operator for manual type checking because the expression x instanceof y merely checks the prototype chain of x for the existence of y (and in
ActionScript 3.0 the prototype chain no longer provides a complete picture of the inheritance hierarchy). The is operator examines the proper inheritance hierarchy and can be used to check not only 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 mySprite and uses the is operator to test whether mySprite is an instance of the Sprite and DisplayObject classes, and whether it implements the IEventDispatcher interface.
ActionScript 3.0 the prototype chain no longer provides a complete picture of the inheritance hierarchy). The is operator examines the proper inheritance hierarchy and can be used to check not only 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 mySprite and uses the is operator to test whether mySprite is an instance of the Sprite and DisplayObject classes, and whether it implements the IEventDispatcher interface.
var mySprite:Sprite = new Sprite();
trace (mySprite is Sprite); // output: true
trace (mySprite is DisplayObject); // output: true
trace (mySprite is IEventDispatcher); // output: true
The is operator checks the inheritance hierarchy and properly reports that mySprite is compatible with the Sprite and DisplayObject classes (the Sprite class is a subclass of the DisplayObject class). The is operator also checks whether mySprite inherits from any classes that implements the IEventDispatcher interface. Because the Sprite class inherits from the EventDispatcher class, which implements the IEventDispatcher interface, the is operator correctly reports that mySprite implements the same interface. The following example shows the same tests from the previous example, but with instanceof instead of the is operator.
The instanceof operator correctly identifies that mySprite is an instance of Sprite, but returns false when used to test whether mySprite is an instance of the DisplayObect class or implements the IEventDispatcher interface.
The instanceof operator correctly identifies that mySprite is an instance of Sprite, but returns false when used to test whether mySprite is an instance of the DisplayObect class or implements the IEventDispatcher interface.
trace (mySprite instanceof Sprite); // output: true
trace (mySprite instanceof DisplayObject); // output: false
trace (mySprite instanceof IEventDispatcher); // output: false