The difference is that apply lets you invoke the function with arguments as an array; call requires the parameters be listed explicitly. A useful mnemonic is "A for array and C for comma."
See MDN's documentation on apply and call.
Pseudo syntax:
theFunction.apply(valueForThis, arrayOfArgs)
theFunction.call(valueForThis, arg1, arg2, ...)
There is also, as of ES6, the possibility to spread the array for use with the call function, you can see the compatibilities here.
Sample code:
function theFunction(name, profession) {
console.log("My name is " + name + " and I am a " + profession +".");
}
theFunction("John", "fireman");
theFunction.apply(undefined, ["Susan", "school teacher"]);
theFunction.call(undefined, "Claude", "mathematician");
theFunction.call(undefined, ...["Matthew", "physicist"]); // used with the spread operator
Answer from flatline on Stack OverflowThe difference is that apply lets you invoke the function with arguments as an array; call requires the parameters be listed explicitly. A useful mnemonic is "A for array and C for comma."
See MDN's documentation on apply and call.
Pseudo syntax:
theFunction.apply(valueForThis, arrayOfArgs)
theFunction.call(valueForThis, arg1, arg2, ...)
There is also, as of ES6, the possibility to spread the array for use with the call function, you can see the compatibilities here.
Sample code:
function theFunction(name, profession) {
console.log("My name is " + name + " and I am a " + profession +".");
}
theFunction("John", "fireman");
theFunction.apply(undefined, ["Susan", "school teacher"]);
theFunction.call(undefined, "Claude", "mathematician");
theFunction.call(undefined, ...["Matthew", "physicist"]); // used with the spread operator
K. Scott Allen has a nice writeup on the matter.
Basically, they differ on how they handle function arguments.
The apply() method is identical to call(), except apply() requires an array as the second parameter. The array represents the arguments for the target method."
So:
// assuming you have f
function f(message) { ... }
f.call(receiver, "test");
f.apply(receiver, ["test"]);
Function invocation...why use .call() and .apply() ?
What is the difference between JavaScript’s `apply` and `call` methods? - TestMu AI Community
What are some use cases for call, apply, and/or bind?
.call vs .bind vs .apply...ELI5?
call calls a function with individual arguments:
fn.call( thisArg, 1, 2, 3 ); // fn( 1, 2, 3 ); with `this` in fn set to `thisArg`
apply calls a function with an array of arguments:
fn.apply( thisArg, [ 1, 2, 3 ] ); // fn( 1, 2, 3 ); with `this` in fn set to `thisArg`
bind doesn't call the function at all, but binds scope and arguments to a function so that when you call the resulting function the original one is called:
var newFn = fn.bind( thisArg, 1, 2, 3 ); // fn doesn't run yet newFn(); // calls fn.call( thisArg, 1, 2, 3 ); newFn( 4 ); // calls fn.call( thisArg, 1, 2, 3, 4 );
So call vs apply just comes down to how you have your data (well that and call is much faster). Use bind when you want to do something later
Videos
Background: I recently had a job interview in which I was asked about the different ways to call a function. I didn't answer correctly because I've essentially ignored the .call() and .apply() options when studying JavaScript. I understand that you can do this, but I can't really think of a scenario that I'd need to use this format.
Is this a pattern that you use/should I be using it? Could someone give an example of when one might want to?