JavaScript Inheritance

by Saksham Malhotra

JavaScript

function shape(name) {
    this.name = name;
}

shape.prototype.printInfo = function() {
    console.log(`This is a ${this.name} with ${this.sides} sides`);
}

function rectangle(name, sides){
    shape.call(this, name);
    this.sides = sides;
}

rectangle.prototype = Object.create(shape.prototype)
rectangle.prototype.constructor = rectangle;

var rect = new rectangle('rectangle', 4)
rect.printInfo();