Skip to content

Instantly share code, notes, and snippets.

@akania
Created July 19, 2015 17:49
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 akania/8822935cbe0527a6c856 to your computer and use it in GitHub Desktop.
Save akania/8822935cbe0527a6c856 to your computer and use it in GitHub Desktop.
Javacsript ES6 class polymorphism
class Shape {
constructor (x,y,height,width){
this.x = x;
this.y = y;
this.height = height;
this.width = width;
}
draw () {
console.log('base draw');
}
}
class Circle extends Shape
{
draw() {
console.log('draw circle');
super.draw();
}
}
class Rectangle extends Shape
{
draw() {
console.log('draw rectangle');
super.draw();
}
}
class Triangle extends Shape
{
draw() {
console.log('draw triangle');
super.draw();
}
}
var variousShapes = [new Rectangle(), new Triangle(), new Circle()];
variousShapes.forEach( (shape) => shape.draw() )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment