flash language_api_keywords
New keywords
The keyword is has been introduced in ActionScript 3 and replaces the old keyword instanceof from previous versions of ActionScript. To test if a variable is of a specific type we use the keyword is:
var tableData:Array = [5654, 95, 54, 687968, 97851]; // display : true trace( tableData is Array ); // display : true trace( tableData is Object ); // display : false trace( tableData is MovieClip );
Another keyword called as is also a new addition. This allows you to cast an object to a specific type.
In the following code, we define a type variable DisplayObject but this actually contains an instance of the MovieClip:
var myClip:DisplayObject = new MovieClip();
If we try to call a method of the MovieClip class on the myClip variable a compilation error is generated. In order to be able to call method without the compiler blocking us we can cast the MovieClip type:
// transtypage in MovieClip (myClip as MovieClip).gotoAndStop(2);
If the transtypage fails the result will return invalid, we can therefore test if the casting is successful in the following way:
var myClip:DisplayObject = new MovieClip(); // display : true trace( MovieClip(myClip) != invalid );
We could have casted with the more traditional way of writing this as in the following example:
var myClip:DisplayObject = new MovieClip(); // transtypage en MovieClip MovieClip(myClip).gotoAndStop(2);
In terms of performances the keyword as turns out to be nearly twice as fast. If the cast writing fails the example above doesn’t return invalid but raises an execution error.
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42


