Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inspect the names/values of arguments in the definition/execution of a JavaScript function

Tags:

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.

like image 866
clemesha Avatar asked May 27 '09 10:05

clemesha


People also ask

What are arguments in a function JavaScript?

arguments is an Array -like object accessible inside functions that contains the values of the arguments passed to that function.

Which keyword is used to access the array of arguments of a function in JavaScript?

You can access specific arguments by calling their index. var add = function (num1, num2) { // returns the value of `num1` console.

What is the type of argument in a function JavaScript?

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.

What are function parameters and arguments in JavaScript?

Function parameters are the names listed in the function definition. Function arguments are the real values passed to (and received by) the function.


1 Answers

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

like image 88
Jonathan Fingland Avatar answered Sep 22 '22 04:09

Jonathan Fingland