View comments | RSS feed

instanceof operator

object instanceof classConstructor

Tests whether object is an instance of classConstructor or a subclass of classConstructor. The instanceof operator does not convert primitive types to wrapper objects. For example, the following code returns true:

new String("Hello") instanceof String;

Whereas the following code returns false:

"Hello" instanceof String;

Availability: ActionScript 1.0; Flash Player 6

Operands

object : Object - An ActionScript object.

classConstructor : Function - A reference to an ActionScript constructor function, such as String or Date.

Returns

Boolean - If object is an instance of or a subclass of classConstructor, instanceof returns true, otherwise it returns false. Also, _global instanceof Object returns false.

See also

typeof operator


Version 8

Comments


thx1138 said on Mar 29, 2006 at 7:10 PM :
Sometimes when you need more information when dealing with components you can also check the className property like so:

// add a TextInput to the stage with an instance name of myTextInput
trace(myTextInput.className); // outputs TextInput
olf said on Mar 26, 2007 at 8:18 AM :
Tip to Flash "instanceof" operator:

Be aware that when trying to see if an object is of e.g. type Array in a trace command, the instanceof checks the whole statement unless you put the object you want to check for in parantheses:

my_array = new Array(5);
trace("Wrong way of using instanceof: " + my_array instanceof Array); // returns "false"
trace("Correct way of using instanceof: " + (my_array instanceof Array)); // returns "Correct way of using instanceof: true"

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00001831.html