Created
November 9, 2016 08:52
-
-
Save reficedev/f2336cd4efb334d857054260cd1feee0 to your computer and use it in GitHub Desktop.
ES2015 : Classes and Inheritance
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 Polygon { | |
constructor(height, width) { // Class constructor | |
this.name = 'Polygon'; | |
this.height = height; | |
this.width = width; | |
} | |
sayName() { // Class method | |
console.log('Hi, I am a', this.name + '.'); | |
} | |
} | |
class Square extends Polygon { | |
constructor(length) { | |
super(length, length); // Call the parent method with super | |
this.name = 'Square'; | |
} | |
get area() { // Calculated attribute getter | |
return this.height * this.width; | |
} | |
} | |
let s = new Square(5); | |
s.sayName(); | |
console.log(s.area); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment