Polymorphism - Javascript

var Shape = function() {

}

Shape.prototype.draw = function() {
return "I am generic shape"
}

var Circle = function() {

}

Circle.prototype = Object.create(Shape.prototype)
Circle.prototype.draw = function() {
return "I am circle"
}

var Square = function() {

}

Square.prototype = Object.create(Shape.prototype)
Square.prototype.draw = function() {
return "I am square"
}

var Triangle = function() {

}

Triangle.prototype = Object.create(Shape.prototype)

//var shapes = [ new Shape(), new Circle, new Square, new Triangle]
var shape = new Shape()
var circle = new Circle()
var square = new Square()
var triangle = new Triangle()

console.log(shape.draw())
console.log(circle.draw())
console.log(square.draw())
console.log(triangle.draw())

Comments

Popular Posts