Created
September 27, 2020 07:55
-
-
Save rahulmalhotra/360777be8204833c2c06c6269f4f1b97 to your computer and use it in GitHub Desktop.
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