Skip to content

Instantly share code, notes, and snippets.

@robotlolita
Forked from koistya/gist:1975645
Created March 5, 2012 00:44
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 robotlolita/1975681 to your computer and use it in GitHub Desktop.
Save robotlolita/1975681 to your computer and use it in GitHub Desktop.
Named parameters application of a function
/**
* Applies a set of named arguments to a `func`.
*
* Example: var f = function(x, y, z) { return x * (y - z); };
* namedApply(f, {x: 1, y: 2, z: 3});
*
* @param f {Function} A function to invoke.
* @param args {Object} A collection of named arguments.
* @return {Function} A new function with partially applied named arguments.
*/
function namedApply(f, args) {
return f.apply(this, argumentsName(f).map(function(name){ return args[name] }))
function argumentsName(f){ return f.toString()
.match(/function[^(]*\((.*?)\)/)[1]
.split(/\s*,\s*/) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment