Getting the Class of an object in AS3

A quick and useful titbit: in order to get the class of an object use the following code:

   1: static function getClass(obj:Object):Class {
   2:     return Class(getDefinitionByName(getQualifiedClassName(obj)));
   3: }

This is great for checking the type of an object.  For example, if you have an object myObj1 and you want to confirm that it is of the same type as myObj2 you can write:

   1: if (myObj1 is getClass(myObj2)) {
   2:     trace("These objects are of the same class!");
   3: }

30 comments

  1. hello man,
    thank you very much for this snippet,is there a similar way to get base class?

    i’d like to know if something i exported to AS from library is a movieclip, when i test it with this function it returns the custom class name i assigned.

    thanks and wonderful work
    michael

  2. Hey Michael,

    This will only return the real class – to get the base class you’ll need to use flash.utils.describeType and then use E4X to get the XML node containing the base class (or something like that). If only the parent class is contained in describeType you might have to recurse down a tree of describeType calls until you reach the base.

    Hope that helps,

    Dave

  3. Hi Michael,
    To find parent class of an objects class use:

    static function getSuperClass(o: Object): Object {
    var n: String = getQualifiedSuperclassName(o);
    if(n == null)return(null);
    return(getDefinitionByName(n));
    }

  4. don’t forget the required includes for this snippet

    import flash.utils.getDefinitionByName;
    import flash.utils.getQualifiedClassName;

    I was surprised that this functionality isn’t built into the Object class.

  5. Dave, great post, and Gabriel Mariani, great addition. I was double checking that I had this right and thought I would share:

    var a:Array = new Array();
    var aClass:Class = Object(a).constructor;
    var aClassByUtils:Class = flash.utils.getDefinitionByName(“Array”) as Class;

    trace(“by consctuctor: ” + aClass);
    trace(“by utils: ” + aClassByUtils);
    trace(“(aClass == Array): ” + (aClass == Array));
    trace(“(aClassByUtils == Array): ” + (aClassByUtils == Array));
    trace(“(aClass == aClassByUtils): ” + (aClass == aClassByUtils));

  6. i’d like to know if something i exported to AS from library is a movieclip, when i test it with this function it returns the custom class name i assigned.

    you can always use the simple “is” operator

    if(obj1 is MovieClip)
    {
    //do something
    }

  7. very very very very very very thank you. i wanted to design columns game and realy need to this code.i wish you success and health and happiness!

  8. Object(obj).constructor doesn’t work for all classes sadly. So the only safe route is the getDefinitionByName(getQualifiedClassName(obj)) route


  9. var a:Array = [2, 3, 5];
    var aClass:Class = Object(a[0]).constructor;
    var aClassByUtils:Class = getDefinitionByName(flash.utils.getQualifiedClassName(a[0])) as Class;

    trace(“by consctuctor: ” + aClass);
    trace(“by utils: ” + aClassByUtils);
    trace(“(aClass == Array): ” + (aClass == Array));
    trace(“(aClassByUtils == Array): ” + (aClassByUtils == Array));
    trace(“(aClass == aClassByUtils): ” + (aClass == aClassByUtils));

    by consctuctor: [class Number]
    by utils: [class int]
    (aClass == Array): false
    (aClassByUtils == Array): false
    (aClass == aClassByUtils): false

    So Def:: Getting qualified name works better

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>