It’s your job to practice these things, you see there’s no way around it, because the challenges could explain this with words - but it wouldn’t click and it wouldn’t stick and it wouldn’t sink in. Same with array methods .map
, .filter
and .reduce
. I can explain what they do, but you won’t fully understand it unless you use those methods yourself many times. You do practice a lot, so just keep going, eventually it will make sense.
Nevertheless, I’ll try to walk you through what this does:
let args = Array.prototype.slice.call(arguments);
The purpose is to transform the arguments
object into an array.
.slice
is a method that is available for arrays, and it returns a new array (what we want, we want to get an array with the arguments as items in the end, right now we have an object with the arguments as items).
Because .slice
is an array method, we can’t use it on anything that’s not an array. This would cause an error:
let args = arguments.slice()
// Uncaught TypeError: arguments.slice is not a function
JavaScript looks at the arguments
object, and we tell it to use an array method on it, which it can’t. It’s like this:
let myLunch = motorcycle.cook()
// Type Error
let myLunch = pasta.cook()
// Fine
Now for the .call
method. You can use it to tell JavaScript what the this
keyword should point at.
let args = Array.prototype.slice .call(arguments);
/\ /\
|| ||
| get me the slice method | and call that method
| from the array prototype | with "arguments" being "this"
Or with my above example:
let myLunch = Food.prototype.cook .call(motorcycle)
Yes JavaScript, I WANT cooked motorcycle for lunch. I know you can’t cook motorcycles, but I’ll tell you how to do it. You’ll take the .cook
method from the Food prototype, and use it with this
being my motorcycle instead of pasta or potato.
Well I hope that made some sense. I don’t think it will at this point, but doesn’t matter. It’s not that important for this challenge.