ActionScript 3.0 - is, instanceof, typeof, as

is - Evaluates whether an object is compatible with a specific data type, class, or interface.

import flash.display.*;
import flash.events.IEventDispatcher;

var mySprite:Sprite = new Sprite();
trace(mySprite is Sprite);           // true
trace(mySprite is DisplayObject);    // true
trace(mySprite is IEventDispatcher); // true
instanceof - Evaluates whether an expression's prototype chain includes the prototype object for function.


var mySprite:Sprite = new Sprite();
trace(mySprite instanceof Sprite);        // true
trace(mySprite instanceof DisplayObject); // true

var mySprite:Sprite = new Sprite();
trace(mySprite instanceof IBitmapDrawable); // false
trace(mySprite is IBitmapDrawable);         // true
typeof - Evaluates expression and returns a string specifying the expression's data type.

trace(typeof Array); // object
trace(typeof Date);  // object
trace(typeof 3);     // number

var a:String = "sample";
var b:String = new String("sample");
trace(typeof a); // string
trace(typeof b); // string
as - Evaluates whether an expression specified by the first operand is a member of the data type specified by the second operand.

public var myArray:Array = ["one", "two", "three"];
trace(myArray as Array);  // one,two,three
trace(myArray as Number); // null
trace(myArray as int);    // null

No comments:

Post a Comment