call, apply & bind
IntermediateHow call, apply and bind choose this for a function, how they differ, and how to write bind yourself with a closure.Builds on The this Keyword, Closures
Empty
Empty
Empty
Empty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
2 min read
On this page
What are call, apply and bind in JavaScript?
call, apply and bind are methods every function inherits from Function.prototype. They let
you choose this for a function yourself, instead of leaving it to how the function is called:
fn.call(thisArg, a, b)runsfnnow, with the arguments listed one by one.fn.apply(thisArg, [a, b])runsfnnow, with the arguments in one array.fn.bind(thisArg, a)does not run anything. It returns a new function whosethisis fixed, and whose first arguments can be filled in ahead of time (partial application).
An easy way to remember the first two: Apply takes an Array, Call takes Commas.
How do call, apply and bind set this?
callandapplyare explicit binding: for that one call,thisis the first argument.bindis a closure aroundapply. The new function remembers the original function, thethisand the preset arguments, and callsfn.apply(thisArg, [...preset, ...args])each time it runs.- A bound function's
thisis permanent. Calling it withcallorapply, or binding it again, does not change it. Onlynewoverrides it, becausenewalways makes a fresh object. - Arrow functions ignore the
thisArgof all three: they have nothisof their own to set.
What mistakes do people make with call, apply and bind?
Mini example
const multiply = (a, b) => a * b;const double = multiply.bind(null, 2); // partial applicationconsole.log(double(5)); // 10 function listArgs() { // Borrow an array method for the array-like `arguments`. return Array.prototype.join.call(arguments, ' + ');}console.log(listArgs(1, 2, 3)); // '1 + 2 + 3'Quick check
If who returns this.id, what does who.bind({ id: "a" }).call({ id: "b" }) return?
Further reading
All steps as text
- Step 1:
The global context starts. Every function inherits from Function.prototype, where the built-in call, apply and bind live.
- Step 2:
Creation phase:
introduceis stored as a function linked to Function.prototype, andaliandhiAliare reserved.Lines 3–18
- Step 3:
alipoints to a new object.Line 6
- Step 4:
Functions are objects.
introduce.callis not on introduce itself, so the lookup follows its [[Prototype]] and finds call on Function.prototype.Line 8
- Step 5:
callruns introduce right away, withthisset to its first argument,ali. The other arguments are passed one by one.Line 8
- Step 6:
this.nameis Ali, so Hi, I'm Ali! is printed.Lines 4–8
- Step 7:
applydoes the same, but takes the arguments as one array and spreads them into the parameters.Line 9
- Step 8:
introduce returns, and console.log prints Hello, I'm Ali.
Lines 4–9
- Step 9:
Now our own bind.
myBindis added to Function.prototype, so every function can use it, like the built-in bind.Lines 11–16
- Step 10:
introduce.myBindis found the same way as call: one step up the chain, on Function.prototype.Line 18
- Step 11:
Inside myBind,
thisis introduce: the object before the dot.ctxis ali, and the rest parameterpresetcollects the extra argument.Line 11
- Step 12:
const fn = thissaves introduce in a variable, because the function returned next will get athisof its own.Line 12
- Step 13:
myBind returns a new function into
hiAli. Its scope stays alive, so the new function keepsfn,ctxandpreset: a closure.Lines 13–18
- Step 14:
hiAli('?')is a plain call, so its ownthisis undefined. That does not matter: it never usesthis.Line 19
- Step 15:
fnis not in hiAli's own scope, so the lookup moves out and finds it in myBind's scope.ctxandpresetare found the same way.Line 14
- Step 16:
fn.apply(ctx, …)calls introduce withthisset to ali and the arguments 'Hey' and '?'.Line 14
- Step 17:
introduce returns, hiAli passes its result back, and Hey, I'm Ali? is printed.
Lines 4–19
- Step 18:
call and apply run a function now, with the
thisyou choose. bind returns a new function withthislocked in, and ours did it with a closure and apply.