Created
July 19, 2015 17:49
-
-
Save akania/8822935cbe0527a6c856 to your computer and use it in GitHub Desktop.
Javacsript ES6 class polymorphism
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
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