Actionscript关联数组构造函数?

时间:2013-07-22 03:38:21

标签: arrays actionscript

这个问题似乎很愚蠢,但有没有办法在变量声明中的actionscript中创建一个关联数组?

e.g。

private var stages:Array = [
    "name" : "NY Stage",
    "location" : "New York",
    "capacity" : 15000
]

相反,我这样做的方式是(1):将数组向上声明,然后在类构造函数中创建数组的其余部分:

private var stages:Array;

public function PlayStage(){
    stages["name"] = "NY Stage";
    stages["location"] = "New York";
    stages["capacity"] = 15000;
}

我可以做一些像top(没有创建对象)的东西吗?

2 个答案:

答案 0 :(得分:3)

不要使用Array来创建关联数组。如果您阅读Array文档,则会特别建议您不要这样做。

请改用Object。以下是有关如何创建关联数组的文档的链接:

Associative arrays in AS3

要遍历关联数组的键(这也用于获取长度),您可以使用:

var oObj:Object = { "name" : "pear", "color" : "yellow" };

...

for ( var key:* in oObj )
{
    // do something with the key or increment a counter, etc.
}

答案 1 :(得分:1)

与xxbbcc一样,关联数组本质上是AS3中的对象,因此速记对象构造将起作用:

private var stages:Object = {
    "name" : "NY Stage",
    "location" : "New York",
    "capacity" : 15000
}