- Both
callandapplymethods serve the same purpose(are functionally identical) - they set thethiscontext for a function and then execute it. - The
bindmethod just sets thethiscontext for a function. We would need to invoke the function separately.
Key difference between
callandapplymethod is that, thecallmethod accepts arguments as individual parameters(i.e., a list of parameters), whileapplyaccepts 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();