javascript可以使用polygon [i] .x而不是polygon [i] [j]?

时间:2013-02-23 12:04:06

标签: javascript multidimensional-array user-defined-types

我正在努力解决这个问题。可以在javascript中创建用户定义,以便我可以使用

polygon[i].x
polygon[i].y

而不是

polygon[i][0]
polygon[i][1]

polygon [i] [j]的代码如下。

var polygon = new Array();
for (i = 0; i < 4; i++)
{
    polygon[i] = new Array(2);
    for (j = 0; j < 2; j++)
    {
        polygon[i][j] = "[" + i + "," + j + "]";
    }
}

for (var i = 0; i < polygon.length; i++)
{
    alert(polygon[i][1]);
}

1 个答案:

答案 0 :(得分:1)

据我所知,你的代码就是这样:

var polygon = [],
    i;

//push an object with x and y into the polygon array
for (i = 0; i < 4; i++){
  polygon.push({
    x : 'x @'+i,
    y : 'y @'+i
  });
}

//accessible as
polygon[index].x
polygon[index].y