10

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?

1 Answer 1

28

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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..)
@mattcodes Yes, they definitely should have done that. JS has a bunch of quirks, and this is one of them.
Also don't assign to arguments[0] etc. as arguments[0] is the exact same thing as your first formal parameter, changing either, will also change the other one.
To be explicit, this isn't specific to Node.js. It's in the JavaScript spec.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.