I was reading the MDN page for the JS Function’s arguments variable:
https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments
I understand that arguments is not an Array so this won’t work:
var a = arguments.slice();
The solution on MDN is do do this:
var args = Array.prototype.slice.call(arguments);
Why use Array.prototype and not just Array.slice.call(arguments)? Is using the prototype significant here?
The
Array.slicemethod is part of the Array and String Generics, a set of “static” methods implemented as properties of theArrayandStringconstructors.Those methods are non-standard, they are available just in Mozilla-based implementations.
I’ve seen a lot confusion between those methods and the standard ones, but you should know that they aren’t the same, if you test:
You will find that they are different methods.
And by the way, if you where using that method, you don’t need to use the
callmethod, the first argument is the object that will be used as thethisvalue (Array.slice(arguments)).Yes, the
Arrayconstructor is just a function, the standardslicemethod, and all the other “array methods” are defined onArray.prototype.This object,
Array.prototype, is the one in which all Array object instances inherit from.As @Pointy says, you could get a reference to the method using an Array instance:
This, in theory, will create a new Array object, and access the
slicemethod up in the prototype chain, but I remember that some implementations are starting to optimize this kind of property access, avoiding the creation of the disposable object, so, at some point, it will be exactly the same also talking in terms of performance.