- Both
call
andapply
methods serve the same purpose(are functionally identical) - they set thethis
context for a function and then execute it. - The
bind
method just sets thethis
context for a function. We would need to invoke the function separately.
Key difference between
call
andapply
method is that, thecall
method accepts arguments as individual parameters(i.e., a list of parameters), whileapply
accepts arguments as a single array
Reference: What is the difference between call() apply() & bind()?
call()
function test(...arguments) {
console.log(this.foo, ...arguments); // bar 10 20
}
test.call({ foo: 'bar' }, 10, 20);
apply()
function test(...arguments) {
console.log(this.foo, arguments); // bar 10 20
}
test.apply({ foo: 'bar' }, 10, 20);
bind()
function test(...arguments) {
console.log(this.foo, ...arguments); // bar 10 20
}
const bindedFn = test.bind({ foo: 'bar' }, 10, 20);
bindedFn();