0

We have a function:

function Foo (a,b,c) {
  return a + b + c;
}

Need to get names of Foo arguments outside from the function. The result should be:

Array['a', 'b', 'c']

Foo.arguments returns null.

The only way I see is calling Foo.toString() and then parsing the result.

Is there another more correct way to get the names?

1 Answers1

1

No, using Function#toString seems to be the way to go:

 const str = Foo.toString();
 const args = str.substr(str.indexOf("("), str.indexOf(")")).split(",").map(it => it.trim());
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151