class Vehicle {
#speed = 0;
constructor(brand) {
this.brand = brand;
}
accelerate(amount) {
this.#speed += amount;
return this;
}
getSpeed() {
return this.#speed;
}
toString() {
return `${this.brand} @ ${this.#speed}km/h`;
}
}
class Car extends Vehicle {
#gear = 1;
constructor(brand, model) {
super(brand);
this.model = model;
}
shiftUp() {
this.#gear++;
return this;
}
toString() {
return `${super.toString()} [Gear ${this.#gear}]`;
}
}
const car = new Car("Toyota", "Supra");
car.accelerate(60).accelerate(40).shiftUp().shiftUp();
console.log(car.toString());
console.log(car instanceof Car);
console.log(car instanceof Vehicle);
console.log(Object.getPrototypeOf(Car) === Vehicle);
Post #3186
2.17K
CHALLENGE
- ❤ 5
- 👍 1