Skip to content

Instantly share code, notes, and snippets.

@reficedev
Created November 9, 2016 08:52
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 reficedev/f2336cd4efb334d857054260cd1feee0 to your computer and use it in GitHub Desktop.
Save reficedev/f2336cd4efb334d857054260cd1feee0 to your computer and use it in GitHub Desktop.
ES2015 : Classes and Inheritance
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