View comments | RSS feed

instanceof

Availability

Flash Player 6.

Usage

object instanceof class

Parameters

object An ActionScript object.

class A reference to an ActionScript constructor function, such as String or Date.

Returns

If object is an instance of class, instanceof returns true; false otherwise. Also, _global instanceof Object returns false.

Description

Operator; determines whether an object belongs to a specified class. Tests whether object is an instance of class.

The instanceof operator does not convert primitive types to wrapper objects. For example, the following code returns true:

new String("Hello") instanceof String;

The following code returns false:

"Hello" instanceof String;

Example

In the following example, a for loop is used to loop through the SWF file and trace only TextInput component instances. The instance names appear in the output.

for (var i in this) {
   if (this[i] instanceof mx.controls.TextInput) {
      trace("Instance \""+i+"\" is a TextInput component instance.");
   }
}

See also

typeof

Comments


TroyWorks said on Aug 26, 2004 at 8:13 PM :
instanceof also appears to work with the parent or abstract type if the class inherits (not sure about interface implements) from a parent class.
rghome said on Oct 31, 2004 at 2:00 PM :
But instanceof seems not to work for the class "String".
No screen name said on Dec 16, 2004 at 8:07 AM :
I've found out that "instanceof" increases compiling time by as much as 50% !! Anyway arround it?
No screen name said on Jan 13, 2005 at 10:51 AM :
instanceof works for determining whether the said class is an interface implementation too
Whittaker007 said on Apr 23, 2005 at 11:51 PM :
instanceof availability is stated as being Flash 6, but the results I get are very different when published for the F6 and F7 projectors.

When checking objects on the root timeline created in the Authoring environment both F6 and F7 report instanceof values correctly. e.g: my_mc instanceof MovieClip; // true
my_btn instanceof Button; // true
my_class instanceof MyClass; // true

But when referring to externally loaded moieclip assets (and after verifying that the loaded .swf has initialized) F7 gives the correct results as expected, but F6 returns false for the intrinsic objects, but still works for custom objects e.g:
_root.load_mc.my_mc instanceof MovieClip; // false
_root.load_mc.my_btn instanceof Button; // false
_root.load_mc.my_class instanceof MyClass; // true

Why is F6 unable to determine instance types of loaded clips, but F7 can?

Scott
No screen name said on Jul 1, 2005 at 11:32 PM :
instanceof is missing from the LiveDocs index.
moloko5 said on Sep 22, 2005 at 9:27 AM :
I've also found that if you want to do an if() statement on instanceof, this syntax:
if(someobj instanceof someclass)
does not work, you have to use the full version:
if(someobj instanceof someclass == true)

 

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

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001371.html