In javascript, there is such thing as arguments
pseudo-parameter, which allows to interact with function arguments dynamically. Now, while I'm listening to the lecture about javascript fundamental things & standards, there was a phrase:
"Don't use arguments
as a writable structure, always treat is as a read-only thing"
I never used arguments
to write there, so it's not a problem for me - but, really - I want to ask my
Question: are there any real use-cases when using arguments
to write there is justified? If not, then why shouldn't arguments
be used to write there?
In javascript, there is such thing as arguments
pseudo-parameter, which allows to interact with function arguments dynamically. Now, while I'm listening to the lecture about javascript fundamental things & standards, there was a phrase:
"Don't use arguments
as a writable structure, always treat is as a read-only thing"
I never used arguments
to write there, so it's not a problem for me - but, really - I want to ask my
Question: are there any real use-cases when using arguments
to write there is justified? If not, then why shouldn't arguments
be used to write there?
4 Answers
Reset to default 8Let's suppose you're mad and you want to customize console.log
so that doing
console.log("window.innerWidth");
would log
window.innerWidth = 775
which seems more convenient than doing
console.log("window.innerWidth =", window.innerWidth);
You could do it "properly", by building a new array or you could reuse arguments
:
(function(){
var stdlog = console.log;
console.log = function(){
if (arguments.length===1 && typeof arguments[0]==="string") {
try {
arguments[1] = eval('('+arguments[0]+')');
arguments[0] += " =";
arguments.length = 2;
} catch (e) {} // in case it can't be evaled, do the normal thing
}
stdlog.apply(console, arguments);
};
})();
console.log("window.innerWidth"); // window.innerWidth = 775
console.log("Hello"); // Hello
Reusing arguments
here would have the advantage of not having to build an array when you don't need it and not repeating the call to stdlog
. The code would be less dry without that hack.
Now, why you should not do this kind of things :
arguments
hasn't been designed to be writable like arrays are, I had to setarguments.length
explictly- it's rather easy to simply slice the
arguments
object and to pass the resulting array as no accessible function needs an arguments object anyway. - using a native structure in a way it was really not intended to be used may lead to future inpatibilities
- it prevents optimizations. JS engines do a lot of optimizations without which many modern web apps wouldn't be possible. They give up optimizing in rare cases and some of them are related to arguments
- it doesn't work the same in strict mode and not strict. Do you really want to have a function which would have a subtly different behavior if added (perhaps by concatenating files in a minification process) to a file having
"use strict"
?
Here's a case in which the strict mode changes everything :
function incrNumber(v) {
arguments[0]++;
console.log(v)
}
In strict mode, the passed value is logged. In non strict mode, the incremented value is logged. And that's the specified behavior.
Sometimes it's convenient to write to arguments if you iterate over the arguments array.
This is a hack, and is not encouraged.
var doSomething = function() {
[].push.call(arguments, "process this too while you're at it");
//arguments is not an array, so [].push.call is used to call push on it.
for (arg in arguments) {
console.log(arguments[arg]);
}
}
This is a hack, and is not encouraged.
I cant't think in any situation it would be useful, since the caller wouldn't be able to see the changes. I thought it would, maybe calling the function using the method apply, but I tried it on Chrome, and the arguments object is not the real list passed to apply.
It depends on your goals ) That's bad idea:
var a = 1;
function b() {
console.log(arguments[0]); // <- 1
arguments[0] = 2;
console.log(arguments[0]); // <- 2
}
b(a);
console.log(a); // <- 1
There aren't any awful things. It's just useless.