我有擁有一些常數和將收到對象文本(關聯陣列)配有一些像這樣的數據的類:對象常量的常量作爲鍵(在密鑰值對)
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);
其實你的第一個例子將編譯沒有雙引號。它們是可選的(只要密鑰遵循標識符命名規則,就像你的那樣)。不幸的是,{}語法不支持這種「替代」。 – 2011-04-04 21:02:27
我不確定,但我想我明白你想要做什麼(有點)。你可能想看看Flash的Dictionary類;它可能會提供您正在尋找的鍵值對。 – 2011-04-04 22:13:56