嘿所以我开始选择Javascript,并且遇到了一些对象的问题。 我正在尝试创建一个需要多个边的形状类。使用这些边,它会创建更多特征,以便存储点的位置坐标。 我现在所拥有的是一个正在考虑大小的类,我想使用for循环来创建存储位置的“属性”。仅仅为了学习目的,我将它们设置为0,以查看是否可以做到这一点。任何澄清对象将不胜感激。试图通过for循环声明一个对象的多个特征
function Shape(size) {
this.size = size
for(var i=0; i<size; i++){ //tries to create the properties
//this[i].posX: 0;
//this[i].posY = 0;
}
}
理想情况下,我想这样是访问他们在这个类型的格式:
var triangle = new Shape(3);
triangle[0].posX = 100; // So essentially I could set this to 100, the integer in the [] would represent a side.
triangle[0].posY = 100; // etc ... for the rest of the sides
的感谢!
是的,我结束了思考数组,但我觉得有可能是一个更简单的方法,然后创建一个新的对象数组。谢谢 – wzsun