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?
Why use Array.prototype and not just Array.slice.call(arguments)?
The Array.slice
method is part of the Array and String Generics, a set of "static" methods implemented as properties of the Array
and String
constructors.
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:
Array.slice === Array.prototype.slice; // false
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 call
method, the first argument is the object that will be used as the this
value (Array.slice(arguments)
).
Is using the prototype significant here?
Yes, the Array
constructor is just a function, the standard slice
method, and all the other "array methods" are defined on Array.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:
[].slice === Array.prototype.slice; // true
This, in theory, will create a new Array object, and access the slice
method 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.
It should be noted that:
var argslice = [].slice.call(arguments);
works too, because if "slice" is a reference to a function on the prototype for the Array constructor then "slice" on a constructed array is (therefore) that very same function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With