Published May 19, 2023
[
 
]
The call()
method calls the function with a given this
value and arguments provided individually.
function Product(name, price) {
this.name = name;
this.price = price;
}
function Food(name, price) {
Product.call(this, name, price);
this.category = 'food';
}
console.log(new Food('cheese', 5).name);
// Expected output: "cheese"
call(thisArg)
call(thisArg, arg1)
call(thisArg, arg1, /* …, */ argN)
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.The result of calling the function with the specified this
value and arguments.