`call` / `apply` / `bind`
Three methods that every function inherits. call and apply invoke a function with a this value you choose; bind returns a new function with that this locked in permanently.
Detailed Explanation
Ordinary JavaScript functions do not own their this. The call site supplies it. These three methods let you supply it deliberately instead of relying on the object-before-the-dot rule.
fn.call(thisArg, a, b, c) invokes fn right now, with this set to thisArg and the remaining arguments passed individually.
fn.apply(thisArg, [a, b, c]) does the same thing but takes the arguments as a single array-like. The only difference from call is argument packaging. A memory aid: a for array, c for comma.
fn.bind(thisArg, a, b) invokes nothing. It returns a brand-new bound function whose this is fixed to thisArg forever, with any extra arguments pre-filled. You call that returned function later.
The classic use of apply was spreading an array into a variadic function: Math.max.apply(null, numbers). Since ES6, the spread operator Math.max(...numbers) does this more clearly, so apply appears mostly in older code. call survives for borrowing methods, most famously Object.prototype.toString.call(value) to get an internal type tag, and Array.prototype.slice.call(arrayLike) to convert an array-like into a real array — though Array.from is now the readable choice.
bind is the one you still reach for daily. Its main job is fixing detached methods. When you pass this.handleClick to addEventListener, the object is lost; this.handleClick.bind(this) keeps it.
bind also does partial application: pre-supply the leading arguments and get back a smaller function. const double = multiply.bind(null, 2) turns a two-argument function into a one-argument one. Arguments you pass at call time are appended after the bound ones.
The rule that surprises people: binding cannot be undone. Calling bound.call(other) does not rebind; the original thisArg still wins. Calling bound.bind(other) creates another wrapper that is still stuck on the first value. There is one exception. new BoundFn() ignores the bound this, because new binding outranks explicit binding, though pre-filled arguments still apply.
Three practical costs. First, bind allocates a new function object every time it runs. Calling .bind(this) inside a render method creates a fresh function on each render, which defeats reference-equality checks in UI frameworks and adds garbage. Bind once, in the constructor or as a class field. Second, bound functions carry a small indirection cost on every call, usually irrelevant. Third, bound.name becomes "bound original", and the bound function has no prototype property, which can confuse reflection code.
Arrow functions solve the same problem lexically and are usually clearer for new code. bind remains necessary when you need the bound value to be something other than the enclosing scope's this, or when you want partial application.
Code Examples
const account = { owner: "Ada", balance: 120 };
function describe(currency, suffix) {
return `${this.owner}: ${currency}${this.balance}${suffix}`;
}
// call: arguments listed one by one.
console.log(describe.call(account, "$", " USD")); // => Ada: $120 USD
// apply: identical, arguments packed in an array.
console.log(describe.apply(account, ["$", " USD"])); // => Ada: $120 USD
// bind: nothing runs yet, you get a new function back.
const describeAccount = describe.bind(account, "$");
console.log(describeAccount(" USD")); // => Ada: $120 USDBinding is permanent, and partial application pre-fills from the left.
// Binding is permanent, and partial application pre-fills from the left.
function multiply(a, b) {
return a * b;
}
const double = multiply.bind(null, 2); // `this` unused, `a` fixed at 2
console.log(double(21)); // => 42
const greet = function () {
return this.tag;
};
const boundA = greet.bind({ tag: "A" });
console.log(boundA.call({ tag: "B" })); // => A (rebinding does nothing)
console.log(boundA.bind({ tag: "B" })()); // => A (still A)
// The exception: `new` outranks a bound `this`.
function Point(x) {
this.x = x;
}
const BoundPoint = Point.bind({ x: 999 });
console.log(new BoundPoint(7).x); // => 7Common Gotchas
`bind` is one-way
a bound function cannot be rebound with `call`, `apply` or another `bind`. Only `new` overrides it.
Binding in a render path leaks allocations
`onClick={this.save.bind(this)}` builds a new function every render, breaking memoisation. Bind once in the constructor or use a class field arrow.
Arrow functions ignore all three
`call`, `apply` and `bind` cannot set `this` on an arrow function, so code that relies on it silently reads the wrong object or throws.
Sloppy mode boxes primitives
in non-strict code, `fn.call("hi")` wraps the string in a `String` object and `null`/`undefined` become `globalThis`. Strict mode passes the value through unchanged.