In ActionScript3, how do you get a reference to an object class?

In ActionScript3, how do you get a reference to an object class?

+6
flash actionscript-3
Jan 22 '09 at 12:29
source share
2 answers

You can use the constructor property if your object was created from a class (from documents: "If the object is an instance of a class, the constructor property contains a reference to the class object. If the object is created using the constructor function, the constructor property contains a reference to the constructor function." ):

 var classRef:Class = myObject.constructor as Class; 

Or you can use flash.utils.getQualifiedClassName() and flash.utils.getDefinitionByName() (not very nice, as this entails unnecessary string manipulation in implementations of these library functions):

 var classRef:Class = getDefinitionByName(getQualifiedClassName(myObject)) as Class; 
+5
Jan 22 '09 at 12:54
source share

It is worth noting that XML objects (XML, XMLList) are an exception to this (i.e. (new XML () as Object) .constructor as class == null). I recommend abandoning getDefinitionByName (getQualifiedClassName) when the constructor does not solve:

 function getClass(obj : Object) : Class { var cls : Class = (obj as Class) || (obj.constructor as Class); if (cls == null) { cls = getDefinitionByName(getQualifiedClassName(obj)); } return cls; } 

Note that getDefinitionByName throws an error if the class is defined in another (including child) application domain from the calling code.

+9
Jan 23 '09 at 8:58
source share



All Articles