I am learning about call
and apply
in javaScript from a online TUT. This function allows more arguments to be passed, rather than having a fixed amount.
var calculate = function(){
var fn = Array.prototype.pop.apply(arguments);
return fn.apply(null, arguments);
};
What I am having difficulty wrapping my head around is this statement.
var fn = Array.prototype.pop.apply(arguments);
The presenter of the of the TUT, explained it as the following:
We are binding the apply
method onto the arguments
object. This is going to give us the Function Object
and assign it to the fn variable. It will also remove the Function Object
from the arguments
Object. Because the Array's pop method
takes the final element in the array, it removes it from the Array and then assigns to what ever called the method. In this case the fn
variable.
What confused me was the following:
We are binding the
apply
method onto thearguments
object. This is going to give us theFunction Object
It will also remove the
Function Object
from thearguments
Object.
And when we write in the return statement:
return fn.apply(null, arguments);
Why are we including null
?