JSFiddle - React, Tailwind, and code Playground

by jnfsmile

JavaScript

var Rectangle = {
    create: function(width, height) {
        var newObj = Object.create(this); // create a new object based on itself
        newObj.width = width;
        newObj.height = height;

        return newObj;
    },
    area: function() {
        return this.width * this.height;
    }
};

var rect1 = Rectangle.create(10, 5);
alert(rect1.area());

var Square = Object.create(Rectangle); // extend Rectangle
Square.create = function(side) {
    // take the create function of Rectangle, or do something totally different
    return Rectangle.create(side, side);
}

var sq = Square.create(10);
alert(sq.area());
debugger;