设置ActionScript对象键对象、ActionScript

由网友(面具背后の殇)分享简介:如果我有一个数组,我可以做的设置键如下:If I have an array, I can set the keys by doing the following:var example:Array = new Array();example[20] = "500,45";example[324] = "432,...

如果我有一个数组,我可以做的设置键如下:

If I have an array, I can set the keys by doing the following:

var example:Array = new Array();

example[20] = "500,45";
example[324] = "432,23";

如果我想要做一些与对象,我将如何实现这一目标?

If I want to do something with Objects, how would I achieve this?

我试过如下:

var example:Object = [{x:500, y:45}, {x:432, y:23}]; // Works but keys are 0 and 1

var example:Object = [20: {x:500, y:45}, 324: {x:432, y:23}]; // Compile errors

var example:Object = [20]: {x:500, y:45}, [324]: {x:432, y:23}; // Compile errors

var example:Object = [20] {x:500, y:45}, [324] {x:432, y:23}; // Compile errors

有没有实现这一目标的好方法?

Is there a good way to achieve this?

我知道我能做到这一点:

I understand I could do this:

var example:Object = {id20 : {x:500, y:45}, id324: {x:432, y:23} };

不过,这并不适合我。

But it doesn't suit me.

推荐答案

[] 符号有做新阵列的含义相同( )所以,当你正在做的:

The [] notation has the same meaning of doing a new Array() so when you are doing:

VAR例如:对象= [{X:500,Y:45},{X:432,Y:23}];

您实际上创建了两个元素谁是对象的数组 {X:500,Y:45} {X:432, Y:23}

you are in fact creating an array with two elements who are object {x:500, y:45} and {x:432, y:23}.

如果你想创建一个对象键20和324使用 {} 符号谁是相同的一个新的对象()

If you want to create an object with key 20 and 324 use the {} notation who is the same a new Object()

所以,你的榜样成为=>

So your example became =>

VAR例如:对象= {20:{X:500,Y:45},324:{X:432,Y:23}};

您可以使用对象作为你的第一个例子做同样的而不是一个阵列

You can do the same as your first example using an Object instead of an Array:

var example:Object = new Object();

example[20] = "500,45";
example[324] = "432,23";
阅读全文

相关推荐

最新文章