Я хочу переписать следующий код:
for(i = 0; i < grounds.length; i++) {
grounds[i].show();
}
с помощью метода forEach:
grounds.forEach(**what should i post here?**);
Полный код:
class Ground {
constructor(x, y, sizeX, sizeY) {
this.x = x;
this.y = y;
this.sizeX = sizeX;
this.sizeY = sizeY;
}
show() {
ctx.fillStyle = "rgb(138, 75, 13)";
ctx.fillRect(this.x, this.y, this.sizeX, this.sizeY);
}
}
}
let ground;
let grounds = [];
function generateGround() {
for(i = 0; i < 10; i++) {
ground = new Ground(0 + i * 40, canvas.height - 30, 40, 30);
grounds.push(ground);
}
}
generateGround();
function draw() {
for(i = 0; i < grounds.length; i++) {
grounds[i].show();
}
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
Я прочитал несколько примеров, но не могу найти способ выполнить метод show () для каждого элемента Ground.