Skip to content

Instantly share code, notes, and snippets.

@stamat
Last active December 31, 2015 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stamat/7976766 to your computer and use it in GitHub Desktop.
Save stamat/7976766 to your computer and use it in GitHub Desktop.
Chain chain chain! Array function chain prototype. Chain the functions in a simple and readable thus elegant way!
Array.prototype.chain = function() {
var res = undefined;
var args = [];
var i = 0;
while (arguments.hasOwnProperty(i)) {
args.push(arguments[i]);
i++;
}
for (var i = 0; i < this.length; i++) {
if(typeof this[i] === 'function') {
if (res !== undefined)
args = [res].concat(args);
res = this[i].apply(this[i], args);
args = [];
} else {
args.push(this[i]);
}
}
return res;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment