最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get function `arguments` as key-value couples object - Stack Overflow

programmeradmin4浏览0评论

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}

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}
Share Improve this question edited Oct 8, 2012 at 14:10 David G 96.8k41 gold badges172 silver badges257 bronze badges asked Oct 8, 2012 at 14:07 etuarduetuardu 5,5363 gold badges51 silver badges63 bronze badges 1
  • If you could describe what it is you're trying to acplish, 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 bee clearer. – Pointy Commented Oct 8, 2012 at 14:11
Add a ment  | 

5 Answers 5

Reset to default 6

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

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

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

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};
}

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.

发布评论

评论列表(0)

  1. 暂无评论