Lesson 56 of 296
call, apply and bind
What you will learn
- How
callandapplyrun a function with a chosenthis - How
bindcreates a new function withthisfixed - Partial application with
bind - How to write your own
bind
On this page
call
call runs a function now, with this set to the first argument. The remaining arguments are passed one by one.
function introduce(city, country) {
return `${this.name} lives in ${city}, ${country}.`;
}
const sam = { name: "Sam" };
introduce.call(sam, "Leeds", "UK"); "Sam lives in Leeds, UK."apply
apply is the same as call, but the arguments are passed as one array.
introduce.apply(sam, ["Leeds", "UK"]); "Sam lives in Leeds, UK."Memory trick: Apply takes an Array; Call takes Commas.
Today, spread syntax often replaces apply:
Math.max.apply(null, [3, 9, 2]); 9 (old way)
Math.max(...[3, 9, 2]); 9 (modern way)bind
bind does not run the function. It returns a new function with this permanently set.
const introduceSam = introduce.bind(sam);
introduceSam("York", "UK"); "Sam lives in York, UK."A bound function's this cannot be changed again, not even with another call or bind. (The only exception is calling it with new.)
Summary
| Method | Runs the function now? | How arguments are given | Returns |
|---|---|---|---|
call | Yes | One by one | The function's result |
apply | Yes | As an array | The function's result |
bind | No | One by one (optional, fixed in advance) | A new function |
Partial application with bind
Arguments given to bind after this are fixed in advance:
function multiply(a, b) {
return a * b;
}
const double = multiply.bind(null, 2);
double(5); 10
double(8); 16null is passed for this because multiply does not use it. This technique is called partial application: fixing some arguments to create a more specific function.
Real uses
- Fixing
thiswhen passing a method as a callback:button.addEventListener("click", obj.handle.bind(obj)). - In older React class components, binding handlers in the constructor.
- Borrowing a method from another object:
Array.prototype.slice.call(arguments)turned array-like objects into arrays in older code. Today you would useArray.from().
Writing your own bind
A common interview task. A simple version:
Function.prototype.myBind = function (context, ...presetArgs) {
const originalFn = this;
return function (...laterArgs) {
return originalFn.apply(context, [...presetArgs, ...laterArgs]);
};
};
const introduceSam = introduce.myBind(sam, "Leeds");
introduceSam("UK"); "Sam lives in Leeds, UK."How it works:
- Inside
myBind,thisis the functionmyBindwas called on (rule 1: method call). - It saves that function and the preset arguments in a closure (Lesson 53).
- It returns a new function that, when called, runs the original with the saved
thisand all arguments combined.
A complete version would also handle being called with new; mentioning that in the interview shows depth.
Common mistakes
- Expecting
bindto run the function. - Mixing up which of
callandapplytakes an array. - Trying to re-bind a bound function.
- Using
bindon arrow functions, which ignore it forthis.
How to say it in an interview
"call and apply invoke a function immediately with a chosen this; call takes arguments individually and apply takes an array. bind returns a new function with this permanently fixed and optionally some arguments preset, which is partial application. Arrow functions ignore all three for this. To implement bind, I return a closure that calls the original function with apply, passing the saved context and the combined arguments."
Key words
| Word | Meaning |
|---|---|
| call | Run a function now with a chosen this and listed arguments |
| apply | Run a function now with a chosen this and an array of arguments |
| bind | Create a new function with this fixed |
| Bound function | A function created by bind |
| Partial application | Fixing some arguments in advance |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.