- Published on
Function.prototype.call()
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"
Syntax
call(thisArg)
call(thisArg, arg1)
call(thisArg, arg1, /* …, */ argN)
Parameters
thisArg
- The value to use as
thiswhen callingfunc. If the function is not instrictmode,nullandundefinedwill 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.