Why does applying the slice method to the javascript arguments value as follows Array.prototype.slice.call(arguments) convert it to an array? If slice is used on arrays, and arguments is not an array, then how does this work? Is it just a special case when slice is applied to arguments?
-
1hrm. yeah. Why wouldn't the conversion be a feature of the arguments object, instead of slice?Michael Paulukonis– Michael Paulukonis2013-01-14 21:52:28 +00:00Commented Jan 14, 2013 at 21:52
-
possible duplicate of how does Array.prototype.slice.call() work?Bergi– Bergi2015-06-29 03:17:19 +00:00Commented Jun 29, 2015 at 3:17
Add a comment
|
2 Answers
From the EcmaScript specification on Array.prototype.slice:
NOTE The
slicefunction is intentionally generic; it does not require that itsthisvalue be an Array object. Therefore it can be transferred to other kinds of objects for use as a method. Whether theslicefunction can be applied successfully to a host object is implementation-dependent.
And so, slice works on every object that has a length property (like Arguments objects). And even for those that do not, it then just returns an empty array.