Found Array.prototype.slice.apply in some inherited code. Why would you use the Array type's prototype method? Apparently to slice Array-like objects that aren't really arrays. Why Array's slice works with the non-but-almost-arrays, I don't know.

Understanding Array.prototype.slice.apply(arguments) in JavaScript ๏ฟฝ Sebastiano Armeli's Tech Blog:

There you go Array.prototype.slice.apply(arguments) converts arguments into an ARRAY.

Here we use one of the methods to call a function in JavaScript, the APPLY method, you can learn more about how to call a function in JS here.
So we apply the slice function to the first argument of the apply function(in this case โ€œargumentsโ€) and we know that the slice() method returns always an Array. We got our Array!


Similarly...

javascript - What's the use of Array.prototype.slice.call(array, 0)? - Stack Overflow:

The DOM usually returns a NodeList for most operations like getElementsByTagName.

Although a NodeList almost feels like an array, it is not. It has a length property like an array does, and a method item(index) to access an object at the given index (also accessible with the [index] notation), but that's where the similarity ends.

So to be able to use the wonderful array methods without rewriting them all for a NodeList, the above line is useful.

Labels: