Skip to content

Instantly share code, notes, and snippets.

@SeanJM
Last active March 15, 2016 18:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SeanJM/03fe217e0c4d08ef9e17 to your computer and use it in GitHub Desktop.
Save SeanJM/03fe217e0c4d08ef9e17 to your computer and use it in GitHub Desktop.
A function which allows the augmentation of an Object with functional methods.
// Here is a JSFiddle with a concrete example: https://jsfiddle.net/SeanJM/rg3ftcgk/1/
// this is one my most leveraged functions that I use to keep my code nice and modular
// -
// Changes made from https://gist.github.com/lewisje/041f4d25d12c8a135a8b
// Based on a post in reddit https://www.reddit.com/r/javascript/comments/46xaln/what_goto_libraries_frameworks_tools_or_otherwise/d08wr56
// from https://www.reddit.com/user/lewisje
function fnChain(target, source, args) {
'use strict';
var name;
function chain(name) {
return function () {
var
n = arguments.length,
a = new Array(n),
i,
b;
for (i = 0; i < n; i++) {
a[i] = arguments[i];
}
b = source[name].apply(null, args.concat(a));
return typeof b === 'undefined' ? target : b;
};
}
if (!Array.isArray(args)) {
throw 'Invalid argument for \'fnChain\', the 3rd argument must be an array of arguments.';
}
for (name in source) {
if (typeof source[name] === 'function' && source.hasOwnProperty(name)) {
target[name] = chain(name);
}
}
return target;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment