TGViewer
JavaScript JavaScript @javascript · 31.1K subscribers
Post #3186 2.17K
CHALLENGE


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);
  • ❤ 5
  • 👍 1
More from @javascript
  1. Sep 22, 2026Post #3511
  2. Sep 22, 2026CHALLENGE function Foo(x) { this.x = x; return { x: x * 2 }; } function Bar(x) { this.x =…
  3. Sep 21, 2026Post #3509
  4. Sep 21, 2026CHALLENGE console.log( typeof typeof 1, 1 + typeof 1, 2 ** 3 ** 2, 1 + 2 + '3', 1 < 2 < 3,…
  5. Sep 20, 2026Post #3507
  6. Sep 20, 2026CHALLENGE 'use strict'; function getValue() { return this.value; } const obj = { value: 42…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →