JSFiddle - React, Tailwind, and code Playground
JavaScript
var chainable = (function (undefined) {
function wrapFn(fn, obj) {
return function () {
var result = this._result = fn.apply(obj, arguments);
return this;
};
}
function Chainable(obj) {
var k, v;
for (k in obj) {
if (typeof (v = obj[k]) !== 'function') continue;
this[k] = wrapFn(obj[k], obj);
}
}
Chainable.prototype.endChain = function () {
return this._result;
};
return function (obj) {
return new Chainable(obj);
};
})();
var o = {
setTest: function (test) {
this.test = test;
},
getTest: function () {
return this.test;
}
};
console.log(chainable(o).setTest('test').getTest().endChain());