Looking at a lot of NodeJS and Javascript code recently, it seems arguments is not an instance of Array but still behaves like one, so people do stuff like Array.prototype.slice.call(arguments, ...)
or [].slice.call(arguments)
which adds verbosity and increases hurdle for newbies to understand etc.. Is there a reason why arguments isnt an instance of Array or is this just one those bad parts?
Add a comment
|
1 Answer
NO. arguments
is a standalone object that just so happens to have a length
property and the ability to use []
to index it. But otherwise, it is just an object, not an Array
object.
And yes, this is indeed one of the bad parts of JavaScript.
-
Thanks for clarification, I know it has extra properties like .callee etc.. but in an ideal world shouldnt they have pointed the prototype to Array. so it would have splice/slice directly on it. I ask not to shoot the JS developer but only to clarify my understanding, ideally someone will turn around and say my idea is retarded becasue of x/y/z (fundamental misunderstanding etc..)user53791– user5379101/28/2011 07:16:29Commented Jan 28, 2011 at 7:16
-
@mattcodes Yes, they definitely should have done that. JS has a bunch of quirks, and this is one of them.Jacob Relkin– Jacob Relkin01/28/2011 07:17:14Commented Jan 28, 2011 at 7:17
-
2Also don't assign to
arguments[0]
etc. asarguments[0]
is the exact same thing as your first formal parameter, changing either, will also change the other one.Ivo Wetzel– Ivo Wetzel01/28/2011 18:30:52Commented Jan 28, 2011 at 18:30 -
2To be explicit, this isn't specific to Node.js. It's in the JavaScript spec.frontendbeauty– frontendbeauty09/19/2012 20:58:26Commented Sep 19, 2012 at 20:58