JavaScript Function

Function.prototype.call, apply, and bind, including the rule that bind does not rebind this and that arrow functions have a lexical this.

模块页正文目前为英文。

Function.prototype.call, apply, and bind are methods of callable ECMAScript functions. They are not a TypeScript this-type, and they are not a way to invoke a method on a DOM node from a page that this reference does not run in a browser.

Examples were checked in Node.js 22 as ES modules. They do not claim a particular browser result.

Examples were checked in Node.js 22, which implements these ECMAScript built-ins. See the MDN documentation for the complete API.

JavaScript tutorial and API overview →

API index

APISummary
Function.prototype.call and applyInvoke a function with a chosen this value. call takes a list of arguments; apply takes an array or array-like list.
Function.prototype.bindReturn a bound function whose this value cannot be rebound. Arrow functions keep a lexical this that bind cannot change.

call and apply

func.call(thisArg, ...args) and func.apply(thisArg, args) invoke func with a chosen this value. call takes arguments as a list. apply takes a single array or array-like args list.

If the value being invoked is not callable, both methods throw TypeError. In strict-mode functions, thisArg is used as given, including null or undefined. These methods do not change an arrow function’s lexical this; that case is on the bind section.

Function.prototype.call and apply

func.call(thisArg, ...args)
func.apply(thisArg, args)

Invoke a function with a chosen this value. call takes a list of arguments; apply takes an array or array-like list.

ParameterDescription
thisArgThe value to use as this when calling func. In strict mode it is used as given.
argscall accepts individual arguments. apply accepts one array or array-like object of arguments.

Returns: The return value of func.

Throws

  • TypeError — Thrown when the value being called is not callable, or when apply’s argument list is not an object.
javascript
function make() {
  return {
    n: 1,
    regular: function (x) { return this.n + x; },
  };
}
const methods = make.call({ n: 7 });
console.log(methods.regular(2));
console.log(methods.regular.call({ n: 10 }, 2));
console.log(methods.regular.apply({ n: 10 }, [3]));
try {
  Function.prototype.call.call(undefined);
} catch (error) {
  console.log(error.name);
}

Expected output

3
12
13
TypeError

Related: bind and arrow functions, TypeScript this types, Official call

MDN documentation →

bind and arrow functions

func.bind(thisArg, ...args) returns a new bound function. The bound this value is stored on that function. Calling the bound function with call, apply, or a second bind does not replace that this value.

An arrow function uses the this of the surrounding scope (its lexical this). call, apply, and bind do not replace that binding. In the example, make.call({ n: 7 }) sets the lexical this for the arrow, so arrow(2) stays 9 even when you later call it with { n: 10 }.

Function.prototype.bind

func.bind(thisArg, ...args)

Return a bound function whose this value cannot be rebound. Arrow functions keep a lexical this that bind cannot change.

ParameterDescription
thisArgThe this value stored on the bound function. Further call, apply, or bind calls do not replace it.
argsOptional arguments prepended when the bound function is later invoked.

Returns: A new bound function. The original function is not modified.

  • A bound function can still be called with extra arguments after the ones supplied to bind.
  • Arrow functions are not a bind shortcut; they close over this from the scope where they were created.
javascript
function make() {
  return {
    n: 1,
    regular: function (x) { return this.n + x; },
    arrow: (x) => this.n + x,
  };
}
const methods = make.call({ n: 7 });
const bound = methods.regular.bind({ n: 20 }, 4);
console.log(bound());
console.log(bound.call({ n: 99 }));
const rebound = bound.bind({ n: 0 });
console.log(rebound());
console.log(methods.arrow(2));
console.log(methods.arrow.call({ n: 10 }, 2));

Expected output

24
24
24
9
9

Related: call and apply, Official bind

MDN documentation →