Glossary / call, apply, and bind
call, apply, and bind
Function.prototype.call, apply, and bind control invocation of traditional functions. call invokes immediately with a chosen this value and positional arguments; apply invokes immediately with an array-like argument list; bind returns a new function with a preset receiver and optional leading arguments. They are useful for adapters and partial application, but arrows keep lexical this regardless.
Syntax and results
function format(prefix, value) {
return `${prefix}: ${this.label}=${value}`;
}
const context = { label: "item" };
format.call(context, "debug", 3); // immediate call
format.apply(context, ["debug", 3]); // immediate call with list
const bound = format.bind(context, "debug");
bound(3); // later call
In non-strict functions, nullish receivers may be replaced with the global object and primitive receivers boxed; strict functions preserve the supplied value. bind fixes this for ordinary calls, and later call/apply cannot replace that bound receiver. When a bound function is used with new, constructor behavior takes precedence over bound this, though preset arguments remain relevant.
Binding creates a distinct function identity. If it is used as an event listener, retain that exact bound function to remove it later, or use an AbortSignal-based listener lifecycle. apply accepts array-like values; spread accepts iterables and is often clearer for arrays. Extremely large argument lists can exceed engine limits.
Choosing the right tool
Use call for immediate explicit invocation, apply for an existing array-like list, and bind when creating a reusable callback. Use closures for more complex adaptation, cancellation, or state. Do not use them to “fix” arrow-function this.
Interview questions
- Which method returns a new function?
bind. - What is the call/apply distinction? Positional arguments versus an array-like argument list.
- Can a bound function’s receiver be changed by call? Not in ordinary invocation.
- How does
newinteract with a bound function? It constructs and ignores the boundthis; bound arguments can still be applied. - Why retain a bound callback reference? Removal APIs need the same function identity that was registered.