Skip to content

Instantly share code, notes, and snippets.

@XOP
Last active November 3, 2015 13:34
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 XOP/fd3541d031ff916df434 to your computer and use it in GitHub Desktop.
Save XOP/fd3541d031ff916df434 to your computer and use it in GitHub Desktop.
function getArgs(func) {
// First match everything inside the function argument parens.
var args = func.toString().match(/function\s.*?\(([^)]*)\)/)[1];
// Split the arguments string into an array comma delimited.
return args.split(',').map(function(arg) {
// Ensure no inline comments are parsed and trim the whitespace.
return arg.replace(/\/\*.*\*\//, '').trim();
}).filter(function(arg) {
// Ensure no undefined values are added.
return arg;
});
}
/*
So given the function above and a sample function, here's how it would work:
*/
function myCustomFn(arg1, arg2,arg3) {
}
console.log(getArgs(myCustomFn)); // ["arg1", "arg2", "arg3"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment