1

In javascript we have the arguments object that is a not quite array that we can query.

How can I get the name of each argument?

For example if I want to know that the 3rd argument is called embedded for example, how would I discover this?

arguments[2].name == "embedded'

Obviously the above does not work.

dagda1
  • 26,856
  • 59
  • 237
  • 450

3 Answers3

1

I'm afraid that's not possible. Only the values themselves are passed:

function logArguments(){
    for(key in arguments)
        console.log(key, arguments[key]);
}
var someObject = {someProperty:false};

logArguments("1", 3, "Look at me I'm a string!", someObject);
// Returns:
// 0 1
// 1 3
// 2 "Look at me I'm a string!"
// 3 Object {someProperty: false}

So you can only get their array indexes.

You can however, use this for(key in arguments){} to supply as many arguments to a function as you'd want.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
0

The arguments object is a list of parameters, it does not store the name of the arguments.

Some browsers let you use the toString method to get the code of a function:

function a(arg1){}
// undefined
a.toString()
// "function a(arg1){}"

If you need named parameters it's common to pass an object:

$.ajax({
  url: "test.html",
  cache: false
})

I'm not sure what you are trying to achieve... if you use positional arguments and the third argument is called "embedded" then the name of arguments[2] will always be "embedded". But while you know that when writing the code the name of the arguments aren't stored anywhere where you can conveniently access them.

Matt Zeunert
  • 16,075
  • 6
  • 52
  • 78
  • `arguments[x].name` does not exist. _"the name of `arguments[3]` will always be "embedded"."_ is not true. – Cerbrus Dec 04 '12 at 14:13
  • @Cerbrus There won't be a name property but I'm assuming the OP is referring to the variable name as set in the function definition. So if you have `function sth(a,b,embedded)` then the "name" of your third argument will always be "embedded". – Matt Zeunert Dec 04 '12 at 14:17
  • 1
    Matt, try it for yourself. There is no way to obtain the name `embedded` aside from converting the function to a string, and manually parsing it. – Cerbrus Dec 04 '12 at 14:18
  • @Cerbrus I completely agree with you. You can't go into the code and retrieve the name of argument number n. But while you can't retrieve it in code you'll always know it in advance because that's how you name arguments: you specify the names of the first, second,... argument in the parentheses after `function`. Maybe I'm misunderstanding the question? – Matt Zeunert Dec 04 '12 at 14:23
  • What the OP wants to do is: When a function is called, the function will have a array `arguments`. He wants to obtain the name of this argument. As programmer, you will know the name, indeed, but there's no way to get in in your code. – Cerbrus Dec 04 '12 at 14:26
  • @Cerbrus Why, yes you're right. I've tried to clarify it a bit in the answer, do you still think it's confusing? – Matt Zeunert Dec 04 '12 at 14:28
0

something like this

function a() {
    var arr = Array.prototype.slice.call(arguments, 0, arguments.length);
    for (var aux in arr) {
        alert(aux + ":" + arguments[aux]);
    }
}

src: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/arguments

Pieter Willaert
  • 879
  • 13
  • 36