Skip to content

Instantly share code, notes, and snippets.

@bettysteger
Last active October 11, 2020 00:05
Show Gist options
  • Save bettysteger/2cadbb7f2e37888ad322 to your computer and use it in GitHub Desktop.
Save bettysteger/2cadbb7f2e37888ad322 to your computer and use it in GitHub Desktop.
ES6 Classes and Inheritance example (www.es6fiddle.net)
/**
* 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