Skip to content

Instantly share code, notes, and snippets.

@yvesvanbroekhoven
Last active September 15, 2016 12:36
Show Gist options
  • Save yvesvanbroekhoven/137f532573d53132c156ebb2a066a68c to your computer and use it in GitHub Desktop.
Save yvesvanbroekhoven/137f532573d53132c156ebb2a066a68c to your computer and use it in GitHub Desktop.
Bind arguments to a function
function Foo(x,y,z) {
bindArguments(this, arguments);
}
function bindArguments(ctx, args) {
var argumentNamesStr = /\((.*)\)/.exec(ctx.toString());
var argumentNames;
if (argumentNamesStr) {
argumentNames = argumentNamesStr[1].split(',');
for (var i=0; i < args.length; i++) {
ctx[argumentNames[i]] = args[i];
}
}
}
var foo = new Foo('a','b','c');
// foo.x === 'a'
// foo.y === 'b'
// foo.z === 'c'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment