How to know if an object is dynamic in AS3

2019-03-31 04:56发布

In Action Script 3, you can write a class that defines a dynamic object (MovieClip and Object are two examples), this objects can be modified in run-time. What I want to know if is there some way (in run-time, of course) to know if certain object is dynamic or not.

PS: Without making something like this:

function isDynamic(object) {
    try {
        object.newProperty = 'someValue'
    } catch (e) {
        return false
    }
    return true
}

4条回答
Deceive 欺骗
2楼-- · 2019-03-31 05:11

Be careful!

Anytime you want to use the describeType() function, please please please use the variation:

import mx.utils.DescribeTypeCache;
var typeDesc:XML = DescribeTypeCache.describeType(object).typeDescription;

Performance of making repeated calls to the runtime reflective machinery will absolutely suck. That's why Adobe invented the DescribeTypeCache class.

查看更多
Ridiculous、
3楼-- · 2019-03-31 05:14

This is a very old post, but I'll add an option for those future searchers.

AS3 has a built in way of doing this:

mx.utils.ObjectUtil.isDynamicObject(yourObject);

Read more about it here.

查看更多
戒情不戒烟
4楼-- · 2019-03-31 05:18

You can use describeType from flash.utils to describe the object in XML form. Here's the reference to the API: flash.utils.describeType

function isDynamic(object) {
    var type:XML = describeType(object);
    if (type.@isDynamic == "true") return true;
    return false;
}
查看更多
萌系小妹纸
5楼-- · 2019-03-31 05:22

CookieOfFortune has the right idea, but unfortunately the code itself has problems, isDynamic is an attribute, and the returned value will be a XMLList with a value of a String that reflects a true or false value, not a child node that directly returns a Boolean. It should look more like this:

function isDynamic(object) : Boolean
{
    var type:XML = describeType(object);
    return type.@isDynamic.toString() == "true";
}
查看更多
登录 后发表回答