JSFiddle - React, Tailwind, and code Playground

by 1337

JavaScript

(function(window) { 
    
    var private = {
        name: 'John'
      };  
    
    function foo() { 
        // if without `new` foo().change() will not work :( (foo.change() will)
        if ( !(this instanceof foo) )
            return new foo();        
        return this;  
    }
    
    foo.change = function(key, val) {
      private[key] = val;
      foo.say();
      return this;  
    };  
    foo.say = function() {
      console.log('Hello '+ private.name);
      return this;
    };
    foo.prototype.change = function(key, val) {
        foo.change(key, val);
        return this;
    };     
    foo.prototype.say = function() {
        foo.say();
        return this;
    };

    window.foo = foo; 
})(window);

foo.say().change('name', 'Ben'); //=> "Hello John" => "Hello Ben"

foo().change('name', 'Mike').change('name', 'Sam');