What is the difference between using call
and apply
to invoke a function?
var func = function() {
alert(\'hello!\');
};
>
The call()
method calls a function with a given this
value and arguments provided individually.
apply()
-
Similar to the call()
method, the first parameter in the apply()
method sets the this
value which is the object upon which the function is invoked. In this case, it's the obj
object above. The only difference between the apply()
and call()
method is that the second parameter of the apply()
method accepts the arguments to the actual function as an array.