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?
Since JavaScript is a scripting language, I feel that its introspection should support getting function parameter names. Punting on that functionality is a violation of first principles, so I decided to explore the issue further.
That led me to this question but no built-in solutions. Which led me to this answer which explains that
arguments
is only deprecated outside the function, so we can no longer usemyFunction.arguments
or we get:Time to roll up our sleeves and get to work:
⭐ Retrieving function parameters requires a parser because complex expressions like
4*(5/3)
can be used as default values. So Gaafar's answer or James Drew's answer are so far the best approaches.I tried the babylon and esprima parsers but unfortunately they can't parse standalone anonymous functions, as pointed out in Mateusz Charytoniuk's answer. I figured out another workaround though by surrounding the code in parentheses, so as not to change the logic:
The newlines prevent issues with
//
(single-line comments).⭐ If a parser is not available, the next-best option is to use a tried-and-true technique like Angular.js's dependency injector regular expressions. I combined a functional version of Lambder's answer with humbletim's answer and added an optional
ARROW
boolean for controlling whether ES6 fat arrow functions are allowed by the regular expressions.Here are two solutions I put together. Note that these have no logic to detect whether a function has valid syntax, they only extract the arguments. This is generally ok since we usually pass parsed functions to
getArguments()
so their syntax is already valid.I will try to curate these solutions as best I can, but without effort from the JavaScript maintainers, this will remain an open problem.
Node.js version (not runnable until StackOverflow supports Node.js):
Full working example:
https://repl.it/repls/SandybrownPhonyAngles
Browser version (note that it stops at the first complex default value):
Full working example:
https://repl.it/repls/StupendousShowyOffices
function parameter string value image dynamically from JSON. Since item.product_image2 is a URL string, you need to put it in quotes when you call changeImage inside parameter.
My Function Onclick
My Function
You can also use "esprima" parser to avoid many issues with comments, whitespace and other things inside parameters list.
It works even with code like this:
The following function will return an array of the parameter names of any function passed in.
Example usage:
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:
I say most cases because there are some things that will trip it up
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:
If Array generics are available, one can use the following instead:
Solution that is less error prone to spaces and comments would be: