In javascript, all functions have a built-in object called the “arguments” object. “Arguments” object is an “Array-like” object, not a real Array.
Array-like objects look like arrays. They have various numbered elements and a length property. But that’s where the similarity stops. Array-like objects do not have any of Array’s functions.
Here is where Array.prototype.slice.call() comes in.
The Array.slice() method returns the selected elements in an array, as a new array object.
Example:
var arr1 = ['free', 'code', 'camp'];
var arr2 = arr1.slice(1, 2);
The result of arr2 will be [‘code’, ‘camp’].
call() and apply() are prototype methods of the Function object, meaning that they can be called on every function in javascript.
We use the method call() of Array.prototype.slice, with “arguments” as “this” parameter, to convert an Array-like object to a real Array.
var args = Array.prototype.slice.call(arguments);
args is now a real Array, and we can use filter(), join(), splice(), etc on it.
SPOILER - Seek and destroy challenge related:
[spoiler]At the time your destroy function is called, you use 3 parameters:
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
But the function only listen to 1 of them:
function destroyer(arr) {
...
}
You can do a console.log(arr) here, and the result will be “[1, 2, 3, 1, 2, 3]”.
So how can we access to “2, 3”, the other two parameters?
We can use the “arguments” object.
The output of console.log(arguments) will be “[1, 2, 3, 1, 2, 3], 2, 3”, who is exactly all given parameters.
Since we want to substract all elements form “[1, 2, 3, 1, 2, 3]” who equals any of the other parámeters, and we know the value of “arr” is “[1, 2, 3, 1, 2, 3]”, we can isolate the remaining “2, 3” to do the filter:
args.splice(0, 1);
The output of console.log(args) is now “2, 3”.
We have now our “arr” ([1, 2, 3, 1, 2, 3]) and our args([2, 3]), so we can apply the filter:
return arr.filter(function(element) {
return args.indexOf(element) === -1;
});
Which means:
For every member (element) of my “arr” ([1, 2, 3, 1, 2, 3]), return only whose not equals any of my “args” ([2, 3]);
And the final result will be [1, 1];[/spoiler]
Hope that helps.