JSFiddle - React, Tailwind, and code Playground

by gschutz

JavaScript

var foo = (function() {
    var instance;
    
    var init = function(a) {
        var that = {};
        var those = that;
        
        a = a || 0;
        
        that.time = a;
        
        that.mo = function() {
            that.time += 1;
            return that;
        }
        
        that.mojo = function(value) {
            value = value || 20;
            var size = value;
            
            those.calc = function(n) {
                size = size - n;
                return those;
            }
            
            those.getSize = function() {
                return size;
            }
            
            those.back = function() {
                return that;
            };
            
            return those;
        }
        
        return that;
    }
    
    return function(a) {
        if( ! instance ) {
            instance = init(a);
        }
        
        return instance;
    };
})();

var giz = {
    that: this,
    time: 0,
    mo: function() {
        this.time += 1;
        return this;
    }
};

console.log(giz.mo().mo().mo().mo());

var f = foo(10);
f.mo().mo().mo().mo()
console.log(f.time);

var g = foo(5);
g.mo().mo().mo();
console.log(g.time);


var size = g.mojo(25).calc(8).back().time;

console.log(size);