Last active
March 15, 2016 18:45
-
-
Save SeanJM/03fe217e0c4d08ef9e17 to your computer and use it in GitHub Desktop.
A function which allows the augmentation of an Object with functional methods.
This file contains hidden or 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
// 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