Created
April 22, 2019 14:58
-
-
Save sdesalas/e362b4b9285c47d0ffffe00c2ae44e46 to your computer and use it in GitHub Desktop.
OO JavaScript with super() in constructor
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
module.exports = class Animal { | |
constructor(energy) { | |
this.energy = energy || 0; | |
} | |
move() { | |
if (this.energy > 0) { | |
console.log('moving...') | |
this.energy = this.energy - 10; | |
} else { | |
console.log('cannot move.. (no energy)') | |
} | |
} | |
eat(food) { | |
this.energy = this.energy + food; | |
} | |
} |
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
const Animal = require('./Animal.js'); | |
module.exports = class Cat extends Animal { | |
constructor(energy) { | |
super(energy) | |
this.sound = 'MEAAAAAAW!!!'; | |
} | |
meaw() { | |
console.log(this.sound) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.