javascript - Using arguments pseudo-parameter as a writeable thing - Stack Overflow
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.
- 软件定义网络:正在进行的网络变革
- 仅售74美元的Android迷你电脑:你会买吗?
- 应用软件战场:安卓不敌苹果
- schema - TALLY XML API ISSUE - Stack Overflow
- python - Problem of redirect when trying to authenticate user using supabase - Stack Overflow
- docker compose - Issues with Using CapSolver Extension in Selenium Grid for reCaptcha v2 - Stack Overflow
- Jetpack Compose TextField keyboard dismisses immediately after typing first letter - Stack Overflow
- node.js - Send GET request with pfx file using AXIOS failed - Stack Overflow
- reactjs - Why can't AWS Amplify find my components from a working version of my React App? - Stack Overflow
- rust - Basic bracket-lib example crashes with “unsafe precondition(s) violated: slice::from_raw_parts” - Stack Overflow
- kotlin - Do I need Serialization to Transfer a MutableMap Between Minecraft Server and Client? If so, How Should I Serialize It?
- How to implement GPU memory recycling in CUDA C++ for data streaming in TensorFlow? - Stack Overflow
- caching - How is the userUX affected by cache purge by a WordPress plugin? Cookie consent settings is not affected? - Stack Over
- html - CSS "vertical-align: middle" doesn't shrink the height of the parent element of img - Stack Ove
- node.js - Response body is null even though server is sending a non null response - Stack Overflow
- tsx - Does Inversify actually require emitDecoratorMetadata for Typescript? - Stack Overflow
- oop - How can I link an object to another object in Java? - Stack Overflow