Skip to content
YouTurkey Tech
All JavaScript topics

call, apply & bind

Intermediate

How 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

Call stack

Empty

Scopes

Empty

Heap

Empty

Console

Empty

0/18Press Play, or step through with the arrows, to begin.
Step 0 of 18

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) runs fn now, with the arguments listed one by one.
  • fn.apply(thisArg, [a, b]) runs fn now, with the arguments in one array.
  • fn.bind(thisArg, a) does not run anything. It returns a new function whose this is 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?

  • call and apply are explicit binding: for that one call, this is the first argument.
  • bind is a closure around apply. The new function remembers the original function, the this and the preset arguments, and calls fn.apply(thisArg, [...preset, ...args]) each time it runs.
  • A bound function's this is permanent. Calling it with call or apply, or binding it again, does not change it. Only new overrides it, because new always makes a fresh object.
  • Arrow functions ignore the thisArg of all three: they have no this of 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
  1. Step 1:

    The global context starts. Every function inherits from Function.prototype, where the built-in call, apply and bind live.

  2. Step 2:

    Creation phase: introduce is stored as a function linked to Function.prototype, and ali and hiAli are reserved.

    Lines 3–18

  3. Step 3:

    ali points to a new object.

    Line 6

  4. Step 4:

    Functions are objects. introduce.call is not on introduce itself, so the lookup follows its [[Prototype]] and finds call on Function.prototype.

    Line 8

  5. Step 5:

    call runs introduce right away, with this set to its first argument, ali. The other arguments are passed one by one.

    Line 8

  6. Step 6:

    this.name is Ali, so Hi, I'm Ali! is printed.

    Lines 4–8

  7. Step 7:

    apply does the same, but takes the arguments as one array and spreads them into the parameters.

    Line 9

  8. Step 8:

    introduce returns, and console.log prints Hello, I'm Ali.

    Lines 4–9

  9. Step 9:

    Now our own bind. myBind is added to Function.prototype, so every function can use it, like the built-in bind.

    Lines 11–16

  10. Step 10:

    introduce.myBind is found the same way as call: one step up the chain, on Function.prototype.

    Line 18

  11. Step 11:

    Inside myBind, this is introduce: the object before the dot. ctx is ali, and the rest parameter preset collects the extra argument.

    Line 11

  12. Step 12:

    const fn = this saves introduce in a variable, because the function returned next will get a this of its own.

    Line 12

  13. Step 13:

    myBind returns a new function into hiAli. Its scope stays alive, so the new function keeps fn, ctx and preset: a closure.

    Lines 13–18

  14. Step 14:

    hiAli('?') is a plain call, so its own this is undefined. That does not matter: it never uses this.

    Line 19

  15. Step 15:

    fn is not in hiAli's own scope, so the lookup moves out and finds it in myBind's scope. ctx and preset are found the same way.

    Line 14

  16. Step 16:

    fn.apply(ctx, …) calls introduce with this set to ali and the arguments 'Hey' and '?'.

    Line 14

  17. Step 17:

    introduce returns, hiAli passes its result back, and Hey, I'm Ali? is printed.

    Lines 4–19

  18. Step 18:

    call and apply run a function now, with the this you choose. bind returns a new function with this locked in, and ours did it with a closure and apply.