Object literal with constants as keys (in key value pair)
I have a class that holds some constants and will receive an object literal (associative array) with some data like this:
var ConfigObj:Config = new Config({
    "Some" : 10,
    "Other" : 3,
    "Another" : 5
});
The class looks like this:
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
  开发者_JS百科  }
}
The problem is, how can I pass the constants as keys like this:
var ConfigObj:Config = new Config({
    Config.SomeProperty : 10,
    Config.OtherProperty : 3,
    Config.AnotherProperty : 5
});
Besides, I would like to keep it inline, if possible.
var MyObj:MyClass = new MyClass({x:1, y:2, z:3});
is, for me, far better than:
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);
I get the feeling that you're over-complicating your configuration object, however if you do want to use constants to set key-value pairs, you'll need to use a temporary variable:
var o:Object, configObj:Config;
o = {};
o[Config.SomeProperty] = 'foo';
o[Config.OtherProperty] = 'bar';
o[Config.AnotherProperty] = 'baz';
configObj = new Config( o );
An important question to ask: are these properties truly constant? If they are, then there's little risk in using the string literal when you instantiate the object:
new Config( { 'SomeProperty':'foo', 'OtherProperty':'bar', 'AnotherProperty':'baz' } );
Of course, this isn't flexible if the values in the constants change.
If you want to enforce type checking and parameter naming you should not use a dynamic class or passing an Object , but you should code a Config class with all the possibilities that are available.
Returning the Config class while setting a paremeter will allow you to inline the call.
It requires more works but it's safer IMHO.
Ex:
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))
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论