[javascript] What's the use of Array.prototype.slice.call(array, 0)?


What's happening here is that Sizzle is creating an actual array out of an array-like object. The array-like object doesn't necessarily have the slice() method, so the prototype method has to be called directly. makeArray() returns a copy of that array-like object that is an actual array, and can be used as such else where.

See here for more information about array-like objects.


I was just browsing Sizzle's source code and I came across this line of code:

array = Array.prototype.slice.call( array, 0 );

I looked up what the function is, but I came to the conclusion that it just returns all elements of the array starting from index 0, and puts the whole into the array, i.e. it doesn't really do anything at all.

What is therefore the use of this line of code? What am I missing?

Edit: It's line 863 from https://github.com/jquery/sizzle/blob/master/sizzle.js#L863.