Functions
call, apply & bind
Explicitly controlling what this refers to.
These three methods let you set this explicitly when calling a function.
call and apply are "do it now with this receiver"; bind is "here is a pre-addressed envelope you can post later".
Key concepts
1
call invokes the function immediately with this and comma-separated arguments. apply is identical but takes arguments as an array. bind does not invoke — it returns a new function permanently bound to the given this.
callapplybindthis
2
bind is the classic fix for passing a method as a callback without losing its receiver, and it also enables partial application by pre-filling arguments.
bind
javascript
function greet(greeting) { return `${greeting}, ${this.name}`; }
const user = { name: 'Ada' };
greet.call(user, 'Hi'); // 'Hi, Ada'
greet.apply(user, ['Hello']); // 'Hello, Ada'
const boundGreet = greet.bind(user);
boundGreet('Hey'); // 'Hey, Ada'