[JS]: class Inheritance Example
JS
09/26/2019
Example 1
JS
class Car { constructor(brand) { this.name = brand } printName() { console.log(this.name) }}
class Model extends Car { constructor(brand, model) { super(brand) this.model = model } printModel() { console.log(this.model) }}
const myCar = new Model("Honda", "Accord")myCar.printName()myCar.printModel()
Output
TEXT
HondaAccord
Example 2
JS
class Car { constructor() { this.name = "Honda" } printName() { console.log(this.name) }}
class Model extends Car { constructor() { super() this.model = "Accord" this.name = "Toyota" } printModel() { console.log(this.model) }}
const myCar = new Model()myCar.printName()myCar.printModel()
Output
TEXT
ToyotaAccord
ES7 Equivalent
JS
class Car { name = "Honda" printName = () => { ... } }
class Model extends Car { model = "Accord" name = "Toyota"
printModel() { ... } }// ...