4

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}
David G
  • 94,763
  • 41
  • 167
  • 253
etuardu
  • 5,066
  • 3
  • 46
  • 58
  • If you could describe what it is you're trying to accomplish, somebody may be able to help. What you're describing seems a little odd to me but if I knew why you want such a facility it might become clearer. – Pointy Oct 08 '12 at 14:11

5 Answers5

6

I wouldn't recommend doing so, because you may lost some data if code will be minified and function arguments will be renamed by compressor/obfuscator.

Inferpse
  • 4,135
  • 1
  • 31
  • 39
  • This is very good advice. It is also the subject of specific technique in the angular community, precisely because of the way it uses its internal annotate function to create an array of function parameters so it knows with which parameters to call a controller / function. This breaks when aggressively minifying development code. – danp Feb 23 '15 at 07:34
4

For some browsers, you can parse the argument names from the defined arguments, based on the toString representation 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 through arguments

Community
  • 1
  • 1
David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • There should probably be a big warning on this answer: doing this is bad practice and fragile. Function parameter names are not meant to be observable from the outside of a function body, and attempting to circumvent this is probably not the way you should be solving whatever issue you're having. – jcalz Apr 16 '21 at 20:54
2

That doesn't exist. You could pass your arguments as an object.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
2

The names of the formal parameters are not available from the runtime (edit except as part of the text in the toString() value of the function, as @David Hedlund points out), but the argument values are available via the arguments object. If you're writing the code in a function then of course you know the names, so you can build such an object yourself:

function foo(a, b, c) {
  return {a: a, b: b, c: c};
}
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • It seems to me that doing it from inside the function is trivial (as you've shown), I think the OP wants to do it from outside the function or as a general method. It is possible to parse the result of *toString* to get the formal parameter names, but you can't get their values, which may change during runtime. – RobG Oct 08 '12 at 21:02
  • @RobG right - I really don't know what's desired; this is one of those things that I've never found myself wanting to do. – Pointy Oct 08 '12 at 21:14
1

Is there a way to obtain an object containing the formal name of each argument as well?

No. Formal parameters are essentially the same as declared variables. Each execution context has an associated variable object (ed3) or environment record (ed 5) that declared variables and formal parameters are made properties of.

You can't access that object, so you can't inspect it to see what properties it has.

In the global execution context, variables are made properties of the global object, but not in a function execution context. And there is no formal method to distinguish between variables and other properties of the global object.

RobG
  • 142,382
  • 31
  • 172
  • 209