I'm looking to do the equivalent of Python's inspect.getargspec()
in Javascript.
I do know that you can get the arguments
list from a Javascript function, but what I'm really looking for is the names of the arguments from the originally defined function.
If this is in fact impossible, I'm going to have to 'brute-force' it by getting the string of the function, i.e. myFunc.toString()
and then parsing out the ...
inside of function myFunc(...)
. Does anyone know how to do this parsing in a general way?
Thanks for any tips.
Here's a version that uses higher order functions. The only thing I don't like about it is that you have to specify parameters as members of an array. But at least they only have to be specified once.
While I can't see any good reason for this,
assuming
func
is the name of your functionThe following function will return an array of parameter names of any function passed in.
Example usage:
Sightly improved version of Jonathan Fingland's answer:
Note: This only works on browsers that support
arguments.callee
.(Also posted at How to get function parameter names/values dynamically from javascript)
here a small nodejs module for that.
https://github.com/kilianc/node-introspect