Basically call()
expects the arguments needs to be passed in a comma separated format, whereas apply()
expects the arguments as an array. So the examples that you have seen might not needed the arguments to be passed as an array. This is a matter of choice,
A simple example:
Math.max.call(Math, 1,2,3,4,5);
//is same as
Math.max.apply(Math, [1,2,3,4,5]);
And in some special cases the this
argument for call
/apply
will be passed as an object
and the original arguments will be ignored. Like,
var x = document.querySelectorAll("div")
Array.prototype.slice.call(x);
//This will convert the nodeList to an array.
The above sample will access the length
property of the passed this
(a nodeList
) and will construct a new array and return it. You have to read the algorithm of slice
to know more about it. And this is how the slice will works internally, An answer by me.