views:

90

answers:

3

If I have a function as such:

function doStuff(arg) {
   arguments[0] = "4" + arg;
   console.log(arg);
   console.log.apply(null, arguments);
}

Clearly doStuff("name") prints "4name" twice.

Do arg and arguments[0] point to the same thing? Can I use them interchangably. Is javascript doing anything internally that I should be aware about and are there any gotchas?

I am aware that arguments deals with all arguments passed not just the named paramaters. I only care about how it deals with the first (or nth) paramater.

[Edit]

I'm actaully using this as

function name(arg) {
   arguments[1] = args;
   arguments[0] = "[INFO] " + args.callee + " arg: %o";
   console.log.apply(this, arguments);
}

and calling name(arguments) in a function itself. Interestingly args.callee prints out the function source code rather then the function name.

+2  A: 

The arguments "array" exists in any function context and enumerates the arguments recieved by the current context call. Yes arguments[0] refers to the first argument given, so if you've labelled that as "name" it will be same thing.

Arguments is powerful for dealing with undefined quantities of arguments (i.e. param lists), meta-programming, or even where you want to impose some heuristic to determine what arguments represent what kind of data (i.e. you don't trust the implementers), but that is a rather limited use case to say the least.

I would strongly advise you avoid relying on the arguments array for two reasons:

1). Index position is not a very safe mechanism to rely on this data actually meaning what you think it is, nor is it a flexible mechanism for handling change.

2). You are effectively destroying information if you drop named params. Although JS function signatures are nothing like as rigid as they are in other languages, it's still critical information for anyone trying to understand what your function actually does. JS is a language where being expressive really really matters.

That said arguments contains other interesting properties such as "callee" which will afford you some considerable power for doing things that can't be accomplished otherwise. That's worth investigating yourself (good starting point here).

There is also the more serious problem that arguments itself is in a strange sort of semi-deprecated, semi-supported state, so where used it does need to be x-browser tested and noted for future consideration.

There are no internal gotchas I'm aware of.

annakata
The `arguments` object itself as local variable within a function is not deprecated.
Tim Down
Thank you, the function will show strange behaviour if not called properly. I can deal with that. I'm using arguments mainly in conjuction with the .apply and arguments.callee function, so it's important to use.
Raynos
A: 

Yes, you can use them interchangeably.

here's a good documentation about how to use arguments: https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments

William
+1  A: 

Yes, arguments[0] and arg point to the same thing and changing one changes the other. ECMAScript 5 (currently gradually making its way into mainstream browsers) changes things slightly: in strict mode the association between named parameters and the arguments object is looser, in that the values in arguments are simply copies of the named parameters. From section 10.6 of ECMAScript 5:

For strict mode functions, the values of the arguments object‘s properties are simply a copy of the arguments passed to the function and there is no dynamic linkage between the property values and the formal parameter values.

Tim Down
Should one as a developer think ahead and try to cope with ECMAscript 5 changes or only fix it when the change in implementation breaks it.
Raynos
@Raynos - always ahead ;) If you know about it then it's definitely a problem you can plan for.
annakata