开发者

Actionscript-3 namespaces and dynamic types?

Okay, so this is a strange one...

Is it possible to add namespaced members to anonymous or dynamic types? Say, if you wanted to "flag" a builtin type as "touched" for example?

Earlier I thought about adding hidden members to StyleSheets and their inner styles and wondered how I'd prevent them being overwritten or serialized or whatever. I gave up because there are plenty of other ways to do what I wanted and deadlines loom - but I'd still like to know if it is at all workable?

I've been trying but I've had no luck...

namespace mynamespace = "http://foo.bar/";
Obj开发者_如何学Cect.prototype.test = "default";
Object.prototype.mynamespace::test = "mynamespace";

var o:Object = new Object();
trace(o.test);
trace(o.mynamespace::test);

On the latest Flex 4 SDK nothing worked for me...


I suggest you do it like this:

package  {
    import flash.utils.Dictionary;
    public class Annotations {
        private static var annotations:Dictionary = new Dictionary(true);
        public static function of(target:Object):Object {
            var ret:Object = annotations[target];
            if (ret == null) annotations[target] = ret = Object;
            return ret;
        }
    }
}

usage

var o:Object = new Object();
Annotations.of(o).foo = 1234;
trace(Annotations.of(o).foo);//1234

please note, this is relatively expensive, but actually performs reasonably well. Both weak key dictionaries as well as static calls are a thing you should avoid in performance critical situations.

greetz
back2dos

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜