20

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.

clemesha
  • 1,908
  • 2
  • 12
  • 12

5 Answers5

24

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

Jonathan Fingland
  • 56,385
  • 11
  • 85
  • 79
  • 1
    Nice, thanks! I do have some good reasons for this :) ...and they are mostly related to wanting to replicate some working Python code that uses `inspect.getargspec()`, in JavaScript. – clemesha May 27 '09 at 10:38
  • 1
    I'm still wondering though if this is the only way of doing such an inspection... there is no more elegant way? – clemesha May 28 '09 at 20:45
  • nothing that I know of, no. It's certainly not a common way to do things in JavaScript. – Jonathan Fingland Nov 30 '09 at 13:29
  • 3
    Note that it doesn't play well at all with minifiers that change variable names, so you should be really carefull when using this kind of hacks. – Grégoire Cachet Dec 17 '09 at 22:16
  • 2
    @Grégoire: Honestly, it isn't a practice I'd advocate at all, but the OP question was fairly clear and this is the only way of getting those argument names. If a minifier is used, all you'll get is the new minified names, which, hopefully, should still be enough. – Jonathan Fingland Dec 18 '09 at 00:15
  • You would need to know parameter names or function names, etc .. if you were building a tool that manipulates the code itself, such as a debugger, or dynamic code generators. The mimifier problem might then be handled using source maps. – Jean Vincent Feb 28 '13 at 10:13
  • The only reason not to go that route is if you anticipate the developer might include comments in the parameter definitions. [Another answer](http://stackoverflow.com/q/1007981#9924463) includes handling for this possibility. – skeggse Oct 22 '13 at 00:59
  • Can't your regexp just be simplified to `/\((.*)\)/` ? – Luke Hutchison Sep 15 '22 at 19:42
5

The following function will return an array of parameter names of any function passed in.

function getParamNames(func) {
    var funStr = func.toString();
    return funStr.slice(funStr.indexOf('(')+1, funStr.indexOf(')')).match(/([^\s,]+)/g);
}

Example usage:

getParamNames(getParamNames) // returns ['func']
getParamNames(function (a,b,c,d){}) // returns ['a','b','c','d']
Jack Allan
  • 14,554
  • 11
  • 45
  • 57
4

here a small nodejs module for that.

https://github.com/kilianc/node-introspect

kilianc
  • 7,397
  • 3
  • 26
  • 37
0

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.

<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
var withNamedParams = function(params, lambda) {
    return function() {
        var named = {};
        var max   = arguments.length;

        for (var i=0; i<max; i++) {
            named[params[i]] = arguments[i];
        }

        return lambda(named);
    };
};

var foo = withNamedParams(["a", "b", "c"], function(params) {
    for (var param in params) {
        alert(param + ": " + params[param]);
    }
});

foo(1, 2, 3);
</script>
</head>
<body>

</body>
</html>
Ionuț G. Stan
  • 176,118
  • 18
  • 189
  • 202
-1

Sightly improved version of Jonathan Fingland's answer:

// Utility function to extract arg name-value pairs
function getArgs(args) {
    var argsObj = {};

    var argList = /\(([^)]*)/.exec(args.callee)[1];
    var argCnt = 0;
    var tokens;

    while (tokens = /\s*([^,]+)/g.exec(argList)) {
        argsObj[tokens[1]] = args[argCnt++];
    }

    return argsObj;
}

// Test subject
function add(number1, number2) {
    var args = getArgs(arguments);
    alert(args.toSource()); // ({number1:3,number2:4})
}

// Invoke test subject
add(3, 4);

Note: This only works on browsers that support arguments.callee.

(Also posted at How to get function parameter names/values dynamically from javascript)

Community
  • 1
  • 1
Ates Goral
  • 137,716
  • 26
  • 137
  • 190