0% found this document useful (0 votes)
3 views5 pages

Function - Prototype.call - JavaScript - MDN

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Function - Prototype.call - JavaScript - MDN

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Web JavaScript Reference Standard built-in objects Function call() English (US)

[Link]()
Baseline Widely available

The call() method of Function instances calls this function with a given this value and arguments provided
individually.

Try it
JavaScript Demo: [Link]()
1 function Product(name, price) {
2 [Link] = name;
3 [Link] = price;
4 }
5
6 function Food(name, price) {
7 [Link](this, name, price);
8 [Link] = "food";
9 }
10
11 [Link](new Food("cheese", 5).name);
12 // Expected output: "cheese"
13

> "cheese"

Syntax
JS
call(thisArg)
call(thisArg, arg1)
call(thisArg, arg1, arg2)
call(thisArg, arg1, arg2, /* …, */ argN)

Parameters
thisArg

The value to use as this when calling func . If the function is not in strict mode, null and undefined will be replaced
with the global object, and primitive values will be converted to objects.

arg1 , …, argN Optional


Arguments for the function.

Return value
The result of calling the function with the specified this value and arguments.

Description
Note: This function is almost identical to apply() , except that the function arguments are passed to call()
individually as a list, while for apply() they are combined in one object, typically an array — for example,
[Link](this, "eat", "bananas") vs. [Link](this, ["eat", "bananas"]) .

Normally, when calling a function, the value of this inside the function is the object that the function was accessed on.
With call() , you can assign an arbitrary value as this when calling an existing function, without first attaching the
function to the object as a property. This allows you to use methods of one object as generic utility functions.

Warning: Do not use call() to chain constructors (for example, to implement inheritance). This invokes the
constructor function as a plain function, which means [Link] is undefined , and classes throw an error
because they can't be called without new . Use [Link]() or extends instead.

Examples
Using call() to invoke a function and specifying the this value
In the example below, when we call greet , the value of this will be bound to object obj , even when greet is not a
method of obj .

JS
function greet() {
[Link]([Link], "typically sleep between", [Link]);
}

const obj = {
animal: "cats",
sleepDuration: "12 and 16 hours",
};

[Link](obj); // cats typically sleep between 12 and 16 hours

Using call() to invoke a function without specifying the first


argument
If the first thisArg parameter is omitted, it defaults to undefined . In non-strict mode, the this value is then substituted
with globalThis (which is akin to the global object).

JS

[Link] = "foo";

function display() {
[Link](`globProp value is ${[Link]}`);
}

[Link](); // Logs "globProp value is foo"

In strict mode, the value of this is not substituted, so it stays as undefined .

JS

"use strict";

[Link] = "foo";

function display() {
[Link](`globProp value is ${[Link]}`);
}

[Link](); // throws TypeError: Cannot read the property of 'globProp' of undefined


Transforming methods to utility functions
call() is almost equivalent to a normal function call, except that this is passed as a normal parameter instead of as the
value that the function was accessed on. This is similar to how general-purpose utility functions work: instead of calling
[Link](callback) , you use map(array, callback) , which allows you to use map with array-like objects that are not
arrays (for example, arguments ) without mutating [Link] .

Take [Link]() , for example, which you want to use for converting an array-like object to a real array. You
could create a shortcut like this:

JS

const slice = [Link];

// …

[Link](arguments);

Note that you can't save [Link] and call it as a plain function, because the call() method also reads its this value,
which is the function it should call. In this case, you can use bind() to bind the value of this for call() . In the following
piece of code, slice() is a bound version of [Link]() , with the this value bound to
[Link]() . This means that additional call() calls can be eliminated:

JS

// Same as "slice" in the previous example


const unboundSlice = [Link];
const slice = [Link](unboundSlice);

// …

slice(arguments);

Specifications
Specification

ECMAScript® 2027 Language Specification


# [Link]

Browser compatibility
Report problems with this compatibility data • View data on GitHub
diordnA rof xoferiF

tenretnI gnusmaS

diordnA weiVbeW

SOi no weiVbeW
diordnA emorhC

diordnA arepO

SOi no irafaS
emorhC

sj. e d o N
xoferiF

arepO

irafaS

oneD
egdE

nuB
call 1 12 1 4 1 18 4 10.1 1 1 4.4 1 1 1 0.10

Full support See implementation notes.

See also
[Link]()
[Link]()
[Link]()
Spread syntax ( ... )
Introduction to Object-Oriented JavaScript

Your blueprint for a better internet.

You might also like