Skip to content

Instantly share code, notes, and snippets.

@r3dm1ke
Created January 12, 2020 17:23
Show Gist options
  • Save r3dm1ke/45025f5ff6bebad27005b7dcd46f1967 to your computer and use it in GitHub Desktop.
Save r3dm1ke/45025f5ff6bebad27005b7dcd46f1967 to your computer and use it in GitHub Desktop.
Car class in ES6
class Car {
constructor(make, model) {
this.make = make;
this.model = model;
}
start() {
console.log('vroom');
}
toString() {
console.log(`Car - ${this.make} - ${this.model}`);
}
}
class SportsCar extends Car {
constructor(make, model, turbocharged) {
super(make, model);
this.turbocharged = turbocharged;
}
start() {
console.log('VROOOOM');
}
}
// Actual usage remains the same
var car = new Car('Nissan', 'Sunny');
car.start(); // vroom
console.log(car.make); // Nissan
var sportsCar = new SportsCar('Subaru', 'BRZ', true);
sportsCar.start(); // VROOOOM
console.log(car.turbocharged); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment