flash - 以常量作为键的对象文字(在键值对中)

标签 flash actionscript-3 object-literal

我有一个包含一些常量的类,并将接收一个对象文字(关联数组)和一些数据,如下所示:

var ConfigObj:Config = new Config({
    "Some" : 10,
    "Other" : 3,
    "Another" : 5
});

这个类看起来像这样:

public dynamic class Config
{
    static public const SomeProperty:String = "Some";
    static public const OtherProperty:String = "OtherProperty";
    static public const AnotherProperty:String = "AnotherProperty";

    public function Config(settings:Object)
    {
        // Doing some stuff here
    }
}

问题是,我怎样才能像这样将常量作为传递:

var ConfigObj:Config = new Config({
    Config.SomeProperty : 10,
    Config.OtherProperty : 3,
    Config.AnotherProperty : 5
});

此外,如果可能的话,我想保持内联。

var MyObj:MyClass = new MyClass({x:1, y:2, z:3});

对我来说,远胜于:

var Temp:Object = new Object();
Temp.x = 1; // Or Temp[x] = 1;
Temp.y = 2; // Or Temp[y] = 2;
Temp.z = 3; // Or Temp[z] = 3;

var MyObj:MyClass = new MyClass(Temp);

最佳答案

我觉得你的配置对象过于复杂了,但是如果你确实想使用常量来设置键值对,你需要使用一个临时变量:

var o:Object, configObj:Config;
o = {};
o[Config.SomeProperty] = 'foo';
o[Config.OtherProperty] = 'bar';
o[Config.AnotherProperty] = 'baz';

configObj = new Config( o );

要问的一个重要问题:这些属性真的是恒定的吗?如果是,那么在实例化对象时使用字符串文字的风险很小:

new Config( { 'SomeProperty':'foo', 'OtherProperty':'bar', 'AnotherProperty':'baz' } );

当然,如果常量中的值发生变化,这是不灵活的。

关于flash - 以常量作为键的对象文字(在键值对中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5544045/

相关文章:

php - Flash AS2 将分数存储在 mysql 数据库中

actionscript-3 - 同时使用两个网络摄像头 AS3?

javascript - 将对象字面量传递给函数内具有变量名的对象

javascript - : mean in jquery? 是什么意思

javascript - 如何从键 :function the same way as key:value in JavaScript? 检索值

flash - 如何保存 .swf 文件的状态?

html - 在 .html 页面上嵌入 .swf 文件

javascript - 将 Flash 时钟转换为 javascript 和 css 时钟相同的图像

arrays - 子类化 AS3 数组 : how to get array elements (this[0] does not work)?

flash - ActionScript 2.0中的声音可视化