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.
arguments is an Array -like object accessible inside functions that contains the values of the arguments passed to that function.
You can access specific arguments by calling their index. var add = function (num1, num2) { // returns the value of `num1` console.
The arguments is an object which is local to a function. You can think of it as a local variable that is available with all functions by default except arrow functions in JavaScript. This object (arguments) is used to access the parameter passed to a function. It is only available within a function.
Function parameters are the names listed in the function definition. Function arguments are the real values passed to (and received by) the function.
While I can't see any good reason for this,
var reg = /\(([\s\S]*?)\)/;
var params = reg.exec(func);
if (params)
var param_names = params[1].split(',');
assuming func
is the name of your function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With