DEV Community

Discussion on: Applying To Facebook

Collapse
 
kennethlum profile image
Kenneth Lum

call and apply are when we have a function, and be able to invoke this function (method) on an object as if this object has this method.

In the old days, we wanted to make the arguments of a function into an array, but that object doesn't have the slice() method. So we can invoke slice on that object using call:

jsfiddle.net/KennethKinLum/dw7jz6na/

const arr1 = Array.prototype.slice.call(arguments);
Enter fullscreen mode Exit fullscreen mode

It is as if we are using arguments.slice() when arguments does not have that method.

And then call and apply just differ in how they take the arguments, as comma separated or as an array.

In the old days when there was no bind() in JS, we can use call to do a binding, because call lets us choose what the this is when running that method -- the same idea as obj.fn().

Collapse
 
bytebodger profile image
Adam Nathaniel Davis

The operative words here are:

In the old days...

Collapse
 
kennethlum profile image
Kenneth Lum • Edited

yeah, some interviewers ask you to see what experience you have with JS, sometimes when you claim you are familiar with vanilla JavaScript. I think it also helps when you read some docs on MDN and about polyfill, sometimes you also see the usage of call and apply, so it is at least good to know what their working principles are.