Get function parameter names for interface purposes

Can someone help me on how to get function parameter names? For instance:

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

I need to get the parameter names outside the function that a , b and c (as literally as strings, for example, to put into the array [" a ", " b ", " c "]), so I can check not only the properties of the object / class in the class with respect to the interface, as well as parameters.


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

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

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

(See implementation details from http://knol.google.com/k/programming-to-the-interface-in-javascript-yes-it-can-be-done-er-i-mean-faked# ) .

However, the implementation above only checks the properties of the object (variables and methods), which is not enough for me.

I need more verification at the parameter level, so that code that implements one interface must follow the methods of the interface, and also follow the parameters from the methods of the interface.


PS. Please do not argue about why to use the interface or why it is useless or useful; instead, it is most useful if you can provide a better interface implementation. The focus here is on what I need to know how to get function parameter names.

+4
source share
4 answers

I doubt that there is any worthy way, since the values โ€‹โ€‹of the javascript parameter can be of different types during transmission, types have no meaning other than order.

This may not be the answer you are looking for, but it can help at least.

 function test(a,b,c,d) { } //this will give us the number of parameters defined in the a function. confirm(test.length); 

To really know the names, you can analyze the definition of a function

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

The following is a simple parsing analysis that I did:

 function test(a,b,c,d) { } var b = function(x,y, z){}; alert(getFnParamNames(b).join(",")); alert(getFnParamNames(test).join(",")); function getFnParamNames(fn){ var fstr = fn.toString(); return fstr.match(/\(.*?\)/)[0].replace(/[()]/gi,'').replace(/\s/gi,'').split(','); } 
+10
source

The only way would be to get the function as a string and use RegExp to get the parameter names:

 function test(a,b,c){/*function body*/} //=> this should return 'a,b,c': test.toString().replace(/(function.+\()(.+(?=\)))(.+$)/,'$2'); 
+1
source

You can use either regex as described here , another alternative would be to use a tokenizer. The following code uses the acorn javascript parser to check if a function contains a specific argument name:

 function isFunction(value) { return value && typeof value === 'function'; } function hasArgument(func, name) { if (!isFunction(func) || !name) { return false; } var tokenizer = acorn.tokenize(func); var token = tokenizer(); while (token.type.type !== 'eof') { // ')' closes function, we do not want to look inside the function body if (token.type.type === ')') { return false; } if (token.type.type === 'name' && token.value === name) { return true; } token = tokenizer(); } return false; 

}

0
source

To make @jerjer's complete answer, we can also guarantee that there is at least one match:

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

Source: https://habr.com/ru/post/1394131/


All Articles