Last active
September 15, 2016 12:36
-
-
Save yvesvanbroekhoven/137f532573d53132c156ebb2a066a68c to your computer and use it in GitHub Desktop.
Bind arguments to a function
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
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