Как правильно указать координаты при унаследовании

Изучаю ООП на 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);


Ответы (2 шт):

Автор решения: Igor
  setPosition(x, y) {
    this.x = 0; - ???
    this.y = 0; - ???
→ Ссылка
Автор решения: alex

Начальные координаты теперь передаются при вызове, если координаты не переданы, через условия передаются х=0 и у=0 в setPosition(x, y). Также и со скоростью, если передано значение меньше 10 или не передано, по умолчанию присвается 10

class AutoVehicle {
  constructor() {
  }
  setPosition(x, y) {
    this.x = x;
    this.y = y;
    if (this.x === undefined) this.x = 0;
    if (this.y === undefined) this.y = 0;
    console.log(`${this.name}'s position is: x = ${this.x}, y = ${this.y}`);
  }
}
class Car extends AutoVehicle {
  constructor(name, x, y) {
    super(x, y);
    this.name = name;
  }
  setSpeed(speed) {
    this.speed = speed;
      if (this.speed === undefined || this.speed < 10) {
        this.speed = 10;
      }
      console.log(this.name + ' is moving at speed ' + this.speed)
  }
  run(x, y) {
    setTimeout(() => {
      while (this.x <= x - this.speed && this.y <= y - this.speed) {
          if (this.x === undefined) {
            this.x = 0;
          }
          this.x += this.speed;
          this.y += this.speed;
          console.log(`${this.name} at x = ${this.x}, y = ${this.y}`)
      }
    }, 1000)
  }
}

const honda = new Car('Honda');
honda.setPosition(30, 40);
honda.setSpeed(60);
honda.run(180, 240);

→ Ссылка