Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.prototype.slice.call(arguments) vs. Array.prototype.slice.apply(arguments)

Previous posts have talked about how Array.prototype.slice.call(arguments) work but I don't get why you're using call instead of apply when apply is used for array-like objects whereas call is used on lists of objects separated by commas. Isn't arguments an array-like object that should used apply instead of call?

like image 996
stackjlei Avatar asked Sep 28 '16 00:09

stackjlei


People also ask

What does array prototype Slice call do?

Array.prototype.slice() The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end ( end not included) where start and end represent the index of items in that array. The original array will not be modified.

What's the difference between array splice () and array slice () methods?

Slice is used to get a new array from the original array whereas the splice is used to add/remove items in the original array. The changes are not reflected in the original array in the case of slice and in the splice, the changes are reflected in the original array.

What is one difference between the splice () and slice () methods?

The slice( ) method copies a given part of an array and returns that copied part as a new array. It doesn't change the original array. The splice( ) method changes an array, by adding or removing elements from it. Note: the Slice( ) method can also be used for strings.

Can slice () be used on strings and arrays?

Slice( ) and splice( ) methods are for arrays. The split( ) method is used for strings.


2 Answers

If you'd like to pass arguments to slice in array instead of one by one, then there is a difference. You could do it this way

[1, 2, 3, 4, 5, 6, 7]  ---- our example arguments
Array.prototype.slice.call(arguments, 2, 5);

here you pass to slice method 2 and 5 and these are slice arguments to indicate that you want to get items at index 1 to item at index. 4. So most likely it will be 3, 4, 5.

Array.prototype.slice.apply(arguments, [2, 5]);

This does the same but arguments for slice can be passed in an array.

like image 146
uRTLy Avatar answered Oct 20 '22 15:10

uRTLy


If x is an array, then these are the same:

// find the "slice" method of an array and call it with "x" as its "this" argument
x.slice();

// find the "slice" method of an array and call it with "x" as its "this" argument
Array.prototype.slice.call(x);

The first argument of func.call is this this argument, or, the value of this inside the function. The remaining arguments are the arguments of the function (in this case there are none).

The slice method, called with no arguments, simply creates a new array with the same contents. That's what we want to do when we do Array.prototype.slice.call(arguments), however, since arguments isn't an array and therefore has no slice method attach to itself, we have to use the second of the two methods to get the slice method of an array, and then apply that to something that's not an array:

// find the "slice" method of an array and call it with "arguments",
// which is *not* an array, as its "this" argument
Array.prototype.slice.call(arguments);
like image 23
Frxstrem Avatar answered Oct 20 '22 14:10

Frxstrem