Изучаю ООП на js, мне нужно указать движение авто в консоле (указав начальные координаты, скорость и конечные координаты). По умолчанию начальные координаты равны 0 и 0, скорость 10. Не получается правильно обратиться к родительским данным. Не смотря на то, что начальные координаты заданы, берутся данные которые стоят по умолчанию. И при движении авто показывает сразу все движение а не посекундно.
Есть класс AutoVehicle который принимает координаты х и у
class AutoVehicle {
constructor(x, y) {
super();
}
setPosition(x, y) {
this.x = 0;
this.y = 0;
console.log(`${this.name}'s position is: x = ${x}, y = ${y}`);
}
}
и класс Car который унаследовал у предыдущего класса свойство
class Car extends AutoVehicle {
constructor(name, x, y) {
super(x, y);
this.name = name;
}
setSpeed(speed) {
this.speed = 10;
this.speed = speed;
console.log(this.name + ' is moving at speed ' + this.speed)
}
run(x, y) {
setTimeout(() => {
while (this.x < x && this.y < y) {
this.x += this.speed;
this.y += this.speed;
console.log(`${this.name} at x = ${this.x}, y = ${this.y}`)
}
}, 1000)
console.log(x, y);
}
}
и при вызове скорость показывает верно, а координаты не меняются
const honda = new Car('Honda');
honda.setPosition(30, 40);
honda.setSpeed(60);
honda.run(180, 240);
В консоле отображается следующее:
Honda's position is: x = 30, y = 40
Honda is moving at speed 60
Honda at x = 60, y = 60
Honda at x = 120, y = 120
Honda at x = 180, y = 180
class AutoVehicle {
constructor(x, y) {
//super();
}
setPosition(x, y) {
this.x = 0;
this.y = 0;
console.log(`${this.name}'s position is: x = ${x}, y = ${y}`);
}
}
//и класс Car который унаследовал у предыдущего класса свойство
class Car extends AutoVehicle {
constructor(name, x, y) {
super(x, y);
this.name = name;
}
setSpeed(speed) {
this.speed = 10;
this.speed = speed;
console.log(this.name + ' is moving at speed ' + this.speed)
}
run(x, y) {
setTimeout(() => {
while (this.x < x && this.y < y) {
this.x += this.speed;
this.y += this.speed;
console.log(`${this.name} at x = ${this.x}, y = ${this.y}`)
}
}, 1000)
console.log(x, y);
}
}
const honda = new Car('Honda');
honda.setPosition(30, 40);
honda.setSpeed(60);
honda.run(180, 240);