Function chaining is somehow getting a popular way to structure your javascript code these days, especially with javascript promises on the rise. Here is a simple tutorial how to do function chaining in javascript.
This is how you can do function chaining using object literal:
var foo = {
test: function () {
console.log('running test');
return this;
},
bar: function () {
console.log('running bar');
return this;
}
};
foo.test().bar();
And here is how you can do function chaining using function constructor:
var foo = function () {
this.test = function () {
console.log('running test');
return this;
};
this.bar = function () {
console.log('running bar');
return this;
};
};
var f = new foo()
f.test().bar();