Is there a way to get the function parameter names of a function dynamically?
Let’s say my function looks like this:
function doSomething(param1, par
The proper way to do this is to use a JS parser. Here is an example using acorn.
const acorn = require('acorn');
function f(a, b, c) {
// ...
}
const argNames = acorn.parse(f).body[0].params.map(x => x.name);
console.log(argNames); // Output: [ 'a', 'b', 'c' ]
The code here finds the names of the three (formal) parameters of the function f
. It does so by feeding f
into acorn.parse()
.