Every function has an object called arguments
, which contains in an array like data structure the arguments which the caller of the function uses.
For example, let that we have the following function:
function sum(a,b){
return a+b;
}
If we call sum
as below:
sum(3,4)
the arguments
would contain two items 3 and 4. (Actually, you could call sum
with 3, 4 or more arguments. All these values would be contained in the arguments).
The key word in the above statement is the array like data structure. arguments
is not an array. So you can't have available the Array's methods (push, shift, slice, etc.)
What does slice
?
The slice() method returns a shallow copy of a portion of an array
into a new array object selected from begin to end (end not included).
The original array will not be modified.
For further info please have a look here.
So if you want to apply
slice on arguments
would could you do?
Since arguments
is not an array, (arguments instanceof Array
returns false), you can't do so like below:
var a = ["zero", "one", "two", "three"];
var sliced = a.slice(1,3);
But you can't do it like below:
Array.prototype.slice.call(arguments, 1);
What does call?
The call() method calls a function with a given this value and
arguments provided individually.
For further info please have a look here.
So, essentially, the following line of code
Array.prototype.slice.call(arguments, 1);
calls the function called slice
on the arguments
objects passing 1 as it's argument. So you get an array with all the arguments except the first.