Skip to the content

Lesson 56 of 296

call, apply and bind

What you will learn

  • How call and apply run a function with a chosen this
  • How bind creates a new function with this fixed
  • 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

MethodRuns the function now?How arguments are givenReturns
callYesOne by oneThe function's result
applyYesAs an arrayThe function's result
bindNoOne 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);        16

null 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 this when 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 use Array.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:

  1. Inside myBind, this is the function myBind was called on (rule 1: method call).
  2. It saves that function and the preset arguments in a closure (Lesson 53).
  3. It returns a new function that, when called, runs the original with the saved this and all arguments combined.

A complete version would also handle being called with new; mentioning that in the interview shows depth.

Common mistakes

  • Expecting bind to run the function.
  • Mixing up which of call and apply takes an array.
  • Trying to re-bind a bound function.
  • Using bind on arrow functions, which ignore it for this.

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

WordMeaning
callRun a function now with a chosen this and listed arguments
applyRun a function now with a chosen this and an array of arguments
bindCreate a new function with this fixed
Bound functionA function created by bind
Partial applicationFixing some arguments in advance

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.