In JavaScript one can get the list of arguments passed to a function via arguments, that is an array-like object:
>> function fun(a, b, c) { console.log(arguments) };
undefined
>> fun(1, 2, 3);
[1, 2, 3]
Is there a way to obtain an object containing the formal name of each argument as well?
That is, if one calls the function like above, he should get:
{a = 1, b = 2, c = 3}
For some browsers, you can parse the argument names from the defined arguments, based on the
toStringrepresentation of your function, as described in this question. You could use that to map the values on your own.Do note that not all parameters will have to be named, for instance, if I were to call your function above,
fun(1, 2, 3, 4)the last parameter would not map to any name, but would still be accessible througharguments