Last active
December 31, 2015 10:59
-
-
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!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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