Skip to content

Instantly share code, notes, and snippets.

@AngeloR
Created December 10, 2019 14:32
Show Gist options
  • Save AngeloR/bc9a64cb75841a74b150deb8b15a1c64 to your computer and use it in GitHub Desktop.
Save AngeloR/bc9a64cb75841a74b150deb8b15a1c64 to your computer and use it in GitHub Desktop.
get all arg names passed to a function
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;
});
}
/*
usage:
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