Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get function parameter names/values dynamically?

Is there a way to get the function parameter names of a function dynamically?

Let’s say my function looks like this:

function doSomething(param1, param2, .... paramN){    // fill an array with the parameter name and value    // some other code  } 

Now, how would I get a list of the parameter names and their values into an array from inside the function?

like image 959
vikasde Avatar asked Jun 17 '09 15:06

vikasde


People also ask

Which of the following will invoke the function after the parameters are recognized by their parameters names?

Keyword arguments will invoke the function after the parameters are recognized by their parameter names.

What are the 2 types of function parameters?

Parameter Types Presently, the type is one of ref, const, or val. The type indicates the relationship between the actual argument and the formal parameter. , for a full discussion of references.)

Can we use static for function parameters?

You are right. I checked the compiler's (Microchip C18) manual and found this: "Function parameters can have storage class auto or static. An auto parameter is placed on the software stack, enabling reentrancy. A static parameter is allocated globally, enabling direct access for generally smaller code."

Can functions share parameter names?

Yes, the names of the variables you pass in a function call can be the same as the names of the parameters in the function definition.


1 Answers

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

var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; var ARGUMENT_NAMES = /([^\s,]+)/g; function getParamNames(func) {   var fnStr = func.toString().replace(STRIP_COMMENTS, '');   var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);   if(result === null)      result = [];   return result; } 

Example usage:

getParamNames(getParamNames) // returns ['func'] getParamNames(function (a,b,c,d){}) // returns ['a','b','c','d'] getParamNames(function (a,/*b,c,*/d){}) // returns ['a','d'] getParamNames(function (){}) // returns [] 

Edit:

With the invent of ES6 this function can be tripped up by default parameters. Here is a quick hack which should work in most cases:

var STRIP_COMMENTS = /(\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s*=[^,\)]*(('(?:\\'|[^'\r\n])*')|("(?:\\"|[^"\r\n])*"))|(\s*=[^,\)]*))/mg; 

I say most cases because there are some things that will trip it up

function (a=4*(5/3), b) {} // returns ['a'] 

Edit: I also note vikasde wants the parameter values in an array also. This is already provided in a local variable named arguments.

excerpt from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments:

The arguments object is not an Array. It is similar to an Array, but does not have any Array properties except length. For example, it does not have the pop method. However it can be converted to a real Array:

var args = Array.prototype.slice.call(arguments); 

If Array generics are available, one can use the following instead:

var args = Array.slice(arguments); 
like image 191
Jack Allan Avatar answered Oct 10 '22 06:10

Jack Allan