Last active
December 22, 2022 06:45
-
-
Save anurag-majumdar/c23ff47481dd75299945053cdc55b389 to your computer and use it in GitHub Desktop.
Inheritance in JavaScript using ES6 keywords super and extends.
This file contains hidden or 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
class Animal { | |
constructor(name, weight) { | |
this.name = name; | |
this.weight = weight; | |
} | |
eat() { | |
return `${this.name} is eating!`; | |
} | |
sleep() { | |
return `${this.name} is going to sleep!`; | |
} | |
wakeUp() { | |
return `${this.name} is waking up!`; | |
} | |
} | |
class Gorilla extends Animal { | |
constructor(name, weight) { | |
super(name, weight); | |
} | |
climbTrees() { | |
return `${this.name} is climbing trees!`; | |
} | |
poundChest() { | |
return `${this.name} is pounding its chest!`; | |
} | |
showVigour() { | |
return `${super.eat()} ${this.poundChest()}`; | |
} | |
dailyRoutine() { | |
return `${super.wakeUp()} ${this.poundChest()} ${super.eat()} ${super.sleep()}`; | |
} | |
} | |
function display(content) { | |
console.log(content); | |
} | |
const gorilla = new Gorilla('George', '160Kg'); | |
display(gorilla.poundChest()); | |
display(gorilla.sleep()); | |
display(gorilla.showVigour()); | |
display(gorilla.dailyRoutine()); | |
// OUTPUT: | |
// George is pounding its chest! | |
// George is going to sleep! | |
// George is eating! George is pounding its chest! | |
// George is waking up! George is pounding its chest! George is eating! George is going to sleep! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment