Arly Arly - 1 year ago 99
Javascript Question

Get function parameter names for interface purposes

can anyone help me on how to retrieve function parameter names? For example:

var A = function(a, b, c) {};

I need to get the parameter names outside the function, which is a, b, and c (as in literally as strings, such as to be put in an array ["a", "b", "c"]), so I can validate not only object/class properties in a class towards an interface, but also the parameters.

Before this question starts raising confusion, let me explain about interface implementation in my term and the reason why I need the answer of the question above:

First of all, I found one interesting way I would like to use in implementing interface by Glen Ford in JavaScript:

for (var member in theInterface) {
if ( (typeof theObject[member] != typeof theInterface[member]) ) {
alert("object failed to implement interface member " + member);
return false;

(Please see the detail about the implementation from

However, the implementation above only validates the object properties (variable and methods) only, which is not enough for me.

I need more validation to parameter level, so that the code that implements one interface should follow the interface methods and also follow the parameters from the interface methods as well.

PS. Please don't argue about why use interface or why it's useless or useful; instead it's most useful if you can provide better interface implementation. The focus here is I just need to know how to get function parameter names.

Answer Source

I doubt that there is any decent way to this, in javascript parameter values can be of different type when passed, the types doesn't matter but the order thus.

This might not be the answer that you are looking for but this could help at least.

function test(a,b,c,d) {


//this will give us the number of parameters defined in the a function.

To really know the names you can parse the function definition

  var str = window['test'].toString();
   //str will be "function test(a,b,c,d){}"

Below is a simple parsing test I made:

function test(a,b,c,d)


var b = function(x,y,  z){};

function getFnParamNames(fn){
    var fstr = fn.toString();
    return fstr.match(/\(.*?\)/)[0].replace(/[()]/gi,'').replace(/\s/gi,'').split(',');