在ActionScript3中,如何获取对象类的引用?

时间:2021-08-28 23:22:50

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

在ActionScript3中,如何获取对象类的引用?

2 个解决方案

#1


You can use the constructor property if your object has been created from a class (from the docs: "If an object is an instance of a class, the constructor property holds a reference to the class object. If an object is created with a constructor function, the constructor property holds a reference to the constructor function."):

如果您的对象是从类创建的,则可以使用构造函数属性(来自docs:“如果对象是类的实例,则构造函数属性包含对类对象的引用。如果使用构造函数创建对象在function中,构造函数属性保存对构造函数的引用。“):

var classRef:Class = myObject.constructor as Class;

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

或者你可以使用flash.utils.getQualifiedClassName()和flash.utils.getDefinitionByName()(这不是一个非常好的方法,因为这需要在这些库函数的实现中进行不必要的字符串操作):

var classRef:Class = getDefinitionByName(getQualifiedClassName(myObject)) as Class;

#2


It's worth noting that the XML objects (XML, XMLList) are an exception to this (ie. (new XML() as Object).constructor as Class == null). I recommend falling back to getDefinitionByName(getQualifiedClassName) when constructor does not resolve:

值得注意的是,XML对象(XML,XMLList)是一个例外(即。(new XML()as Object).constructor as Class == null)。我建议在构造函数不解析时回退到getDefinitionByName(getQualifiedClassName):

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 will throw an error if the class is defined in a different (including a child) application domain from the calling code.

请注意,如果在调用代码的不同(包括子级)应用程序域中定义了类,则getDefinitionByName将引发错误。

#1


You can use the constructor property if your object has been created from a class (from the docs: "If an object is an instance of a class, the constructor property holds a reference to the class object. If an object is created with a constructor function, the constructor property holds a reference to the constructor function."):

如果您的对象是从类创建的,则可以使用构造函数属性(来自docs:“如果对象是类的实例,则构造函数属性包含对类对象的引用。如果使用构造函数创建对象在function中,构造函数属性保存对构造函数的引用。“):

var classRef:Class = myObject.constructor as Class;

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

或者你可以使用flash.utils.getQualifiedClassName()和flash.utils.getDefinitionByName()(这不是一个非常好的方法,因为这需要在这些库函数的实现中进行不必要的字符串操作):

var classRef:Class = getDefinitionByName(getQualifiedClassName(myObject)) as Class;

#2


It's worth noting that the XML objects (XML, XMLList) are an exception to this (ie. (new XML() as Object).constructor as Class == null). I recommend falling back to getDefinitionByName(getQualifiedClassName) when constructor does not resolve:

值得注意的是,XML对象(XML,XMLList)是一个例外(即。(new XML()as Object).constructor as Class == null)。我建议在构造函数不解析时回退到getDefinitionByName(getQualifiedClassName):

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 will throw an error if the class is defined in a different (including a child) application domain from the calling code.

请注意,如果在调用代码的不同(包括子级)应用程序域中定义了类,则getDefinitionByName将引发错误。