views:

27

answers:

1

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/";
Object.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...

+1  A: 

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

back2dos
That's a much better than actually modifying the target. I feel dumb for not thinking of it sooner, thanks back2dos.
Ben