ActionScript的3命名空间和动态类型? [英] Actionscript-3 namespaces and dynamic types?

查看:154
本文介绍了ActionScript的3命名空间和动态类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以这是一个奇怪的......

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?

前面我想过将隐藏的成员样式表和自己内心的风格,不知道我怎么会prevent他们被覆盖或序列化或什么的。我放弃了,因为还有很多其他的方法可以做到我想要的东西和最后期限迫在眉睫 - ?但我仍想知道,如果它是在所有可行的

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);

在最新的Flex 4 SDK没有什么工作对我来说...

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

推荐答案

我建议你做这样的:

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;
        }
    }
}

用法

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.

格尔茨
back2dos

greetz
back2dos

这篇关于ActionScript的3命名空间和动态类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆