JS Prototypes

by toubia95

HTML

<p id="result"></p>

JavaScript

var res = document.getElementById("result");

var date1 = new Date();

Date.prototype.month = function () {
    return this.getMonth() + 1;
};

var month1 = date1.month();

res.insertAdjacentHTML('beforeend', date1 + '<br>');
res.insertAdjacentHTML('beforeend', month1 + '<br>');

function Multiply(x) {
    this.by = function (y) {
        return x * y;
    }
}

Multiply.prototype.plus = function (x) {
    return this + x;
}

var mult = new Multiply(3).by(7);
res.insertAdjacentHTML('beforeend', mult + '<br>');

var mult2 = new Multiply(3).plus(5);
res.insertAdjacentHTML('beforeend', mult2 + '<br>');

var Fonc = Fonc || {
    Dec: function (x) {
        this.dec = x;
        return this;
    },
    Mult: function (y) {
        this.mult = y;
        this.decmult = this.dec * y;
        return this;
    },
    Display: function () {
        if (this.decmult) {
            return this.decmult;
        } else if (this.dec) {
            return this.dec;
        } else if (this.mult) {
            return this.mult;
        } else {
            return 0;
        };
    }
};
console.log(Fonc.Dec(3).Mult(9).decmult);
mult3 = Fonc.Dec(3).Mult(9).Display();
res.insertAdjacentHTML('beforeend', mult3 + '<br>');