JSFiddle - React, Tailwind, and code Playground

by Leo Cavalcante

JavaScript

'use strict';

var mixinPoint = (function(){
    var staticForPointMixins = 0;    
    
    function define(x, y) {
        this.x = x;
        this.y = y;
    }
    
    function add(point) {
        this.x += point.x;
        this.y += point.y;
    }
    
    function subtract(point) {
        this.x -= point.x;
        this.y -= point.y;
    }
    
    function incrementStatic() {
        return ++staticForPointMixins;
    }
    
    return function(proto) {
        proto.define = define;
        proto.add = add;
        proto.subtract = subtract;
        proto.incrementStatic = incrementStatic;
    };
}());

var mixinComplex = (function(){
    function multiply(point) {
        this.x *= point.x;
        this.y *= point.y;
    }
    function toString() {
        return '['+this.x+', '+this.y+']';
    }
    return function(proto){
        proto.multiply = multiply;
        proto.toString = toString;
    };
}());

var math = {
    Point: function(x, y){
        this.x = x;
        this.y = y;
    },
    Complex: function(){
        math.Point.apply(this, arguments);
    }
};

mixinPoint(math.Point.prototype);
mixinPoint(math.Complex.prototype);
mixinComplex(math.Complex.prototype);

var p = new math.Point(2,2);
var c = new math.Complex(3,3);

p.add(new math.Point(1,1));
c.multiply(p);

console.log(p.incrementStatic(), c.incrementStatic());

console.log(p.add === c.add);
console.log(p.subtract === c.subtract);

console.log(p, c);