This code snippet is used in ES6 Inheritance in Classes JavaScript Tutorial by SFDC Stop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
// * Inheritance in Classes in ES6 | |
// * Class is a user defined type with some data members and member functions | |
// * Good to keep all the reusable code in the parent class and re-use it in the child class | |
class Phone { | |
constructor(name, price) { | |
this.name = name; | |
this.price = price; | |
} | |
printName() { | |
console.log(this.name); | |
} | |
printPrice() { | |
console.log(this.price); | |
} | |
} | |
// * Moving from Generalisation to Specialization | |
class TouchPhone extends Phone { | |
constructor(name, price, touchSensitivity) { | |
super(name, price); | |
this.touchSensitivity = touchSensitivity; | |
} | |
printTouchSensitivity() { | |
console.log(this.touchSensitivity); | |
} | |
} | |
let iPhone = new TouchPhone('iPhone11', 200, 1.5); | |
iPhone.printName(); | |
iPhone.printPrice(); | |
iPhone.printTouchSensitivity(); | |
console.log('---------------------------------'); | |
let basicPhone = new Phone('Basic Phone', 100); | |
basicPhone.printName(); | |
basicPhone.printPrice(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment