Last active
October 11, 2020 00:05
-
-
Save bettysteger/2cadbb7f2e37888ad322 to your computer and use it in GitHub Desktop.
ES6 Classes and Inheritance example (www.es6fiddle.net)
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
/** | |
* Classes and Inheritance | |
* Code Example from http://www.es6fiddle.net/ | |
*/ | |
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=10) { // ES6 features Default Parameters | |
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(); // => Hi, I am a Square. | |
console.log(s.area); // => 25 | |
console.log(new Square().area); // => 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment