JavaScript Reference ยท Function
Function.prototype.call()
Reference for JavaScript Function.prototype.call(): run a function immediately with a chosen this value and separate arguments, with practical uses.
call() invokes a function right away with an explicit this value and with arguments passed one by one. It lets you borrow a method for an object that does not own it, or run a plain function as if it were a method.
It is the immediate cousin of bind() (which returns a new function) and of apply() (which takes the arguments as an array).
fn.call(thisArg, arg1, arg2);
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
fn.call(thisArg) fn.call(thisArg, arg1, arg2, /* ..., */ argN)
| Name | Description |
|---|---|
thisArg |
The value to use as this inside the call. |
arg1, ..., argN |
Arguments passed to the function, in order. |
Return value: whatever the function returns.
Exceptions: a TypeError if call is used on a non-function. Any error thrown by the function itself passes through.
Usage notes
thisin strict mode is exactlythisArg, even a primitive orundefined. In sloppy mode,nullandundefinedbecome the global object and primitives are wrapped in objects.- Arrow functions ignore
thisArg. Theirthisis fixed where they are written; only the arguments are passed. - Borrowing array methods for array-likes such as
arguments, DOM collections or{ length }objects was common. TodayArray.from()or spread is usually clearer. - Calling a parent method in old-style inheritance:
Parent.call(this, name)runs a constructor on the current object. Classes usesuper()instead. - Safe method access:
Object.prototype.hasOwnProperty.call(obj, key)works even ifobjdefines its ownhasOwnPropertyor has no prototype. - Spread instead of
apply(). When you have an array of arguments,fn.call(ctx, ...args)replacesfn.apply(ctx, args). - Performance is comparable to a normal call in modern engines.
Browser support
call() is available in every browser and JavaScript runtime.