使用常量作为键的对象文字(在键值对中)

我有一个包含一些常量的类,它将接收一个对象文字(关联数组),其中包含如下数据:
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' } );
当然,如果常量中的值发生变化,则这不灵活。     
如果你想强制执行类型检查和参数命名,你不应该使用
dynamic class
或传递
Object
,但你应该编写一个
Config class
,其中包含所有可用的可能性。 在设置参数时返回
Config class
将允许您内联呼叫。 它需要更多的工作,但它更安全恕我直言。 例如:
class Config {
    protected var _somePropertySet:Boolean
    public function get isSomePropertySet():Boolean{
        return _somePropertySet
    }
    protected var _someProperty:String;
    public function setSomeProperty(value:String):Config{
        _somePropertySet=true
        _someProperty = value
        return this
    }
    public function get someProperty():String{
        return _someProperty
    }

    protected var _someOtherPropertySet:Boolean
    public function get isSomeOtherPropertySet():Boolean{
        return _someOtherPropertySet
    }
    protected var _someOtherProperty:int;
    public function setSomeOtherProperty(value:int):Config{
        _someOtherPropertySet=true
        _someOtherProperty = value
        return this
    }

    protected var _someAnotherPropertySet:Boolean
    public function get isSomeAnotherPropertySet():Boolean{
        return _someAnotherPropertySet
    }
    protected var _someAnotherProperty:Object;
    public function setSomeAnotherProperty(value:Object):Config{
        _someAnotherPropertySet=true
        _someAnotherProperty = value
        return this
    }
}

class Tmp {
    public function Tmp(config:Config) {
        initFromConfig(config)
    }

    protected function initFromConfig(config:Config):void {
        if (config.isSomePropertySet){
            //..
        }
        if (config.isSomeOtherPropertySet){
            //..
        }
        if (config.isSomeAnotherPropertySet){
            //..
        }
    }
}
var t:Tmp=new Tmp(new Config().setSomeProperty("foo").setSomeOtherProperty(5).setSomeAnotherProperty(null))
    

要回复问题请先登录注册