0
我有一個對象數組,我試圖遍歷數組中的每個對象。我需要在每個對象上調用一個方法。我應該如何去做這件事?迭代js中的數組屬性方法
function basicBox(x,y,width,height,color) {
this.color = color;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.draw = function() {
g.drawStyle = this.color;
g.drawRect(this.x,this.y,this.width,this.height);
}
}
var boxes = [b1,b2,b3];
function run() {
for (var i = 0; i < boxes.length; ++i) {
boxes[i].update();
}
}
請參閱:http://stackoverflow.com/questions/684672/loop-through-javascript-object – 2014-09-20 04:02:23
'update'或'draw'?什麼是'g'? – elclanrs 2014-09-20 04:04:21