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?
I don't know how to get a list of the parameters but you can do this to get how many it expects.
It's pretty easy.
At the first there is a deprecated
arguments.callee
— a reference to called function. At the second if you have a reference to your function you can easily get their textual representation. At the third if you calling your function as constructor you can also have a link via yourObject.constructor. NB: The first solution deprecated so if you can't to not use it you must also think about your app architecture. If you don't need exact variable names just use inside a function internal variablearguments
without any magic.https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments/callee
All of them going to call toString and replace with re so we can create a helper:
Some examples:
Enjoy with JS!
UPD: Jack Allan was provided a little bit better solution actually. GJ Jack!
I know this is an old question, but beginners have been copypasting this around as if this was good practice in any code. Most of the time, having to parse a function's string representation to use its parameter names just hides a flaw in the code's logic.
Parameters of a function are actually stored in an array-like object called
arguments
, where the first argument isarguments[0]
, the second isarguments[1]
and so on. Writing parameter names in the parentheses can be seen as a shorthand syntax. This:...is the same as:
The variables themselves are stored in the function's scope, not as properties in an object. There is no way to retrieve the parameter name through code as it is merely a symbol representing the variable in human-language.
I always considered the string representation of a function as a tool for debugging purposes, especially because of this
arguments
array-like object. You are not required to give names to the arguments in the first place. If you try parsing a stringified function, it doesn't actually tell you about extra unnamed parameters it might take.Here's an even worse and more common situation. If a function has more than 3 or 4 arguments, it might be logical to pass it an object instead, which is easier to work with.
In this case, the function itself will be able to read through the object it receives and look for its properties and get both their names and values, but trying to parse the string representation of the function would only give you "obj" for parameters, which isn't useful at all.
Note: if you want to use ES6 parameter destructuring with the top solution add the following line.
How I typically do it:
You can even ref the args by the functions name like:
Hope this helps!
You can access the argument values passed to a function using the "arguments" property.