我正在为这个概念而挣扎。是可以在javascript中创建一个用户定义的,这样我就可以使用了
polygon[i].x
polygon[i].y
而不是
polygon[i][0]
polygon[i][1]
多边形[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]);
}
就我所能理解的代码而言,这就是:
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