Skip to content

Instantly share code, notes, and snippets.

@rahulmalhotra
Created September 27, 2020 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulmalhotra/360777be8204833c2c06c6269f4f1b97 to your computer and use it in GitHub Desktop.
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
'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