嘿所以我開始選擇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