JavaScript Function.prototype.call() Method
Last Updated :
24 Jan, 2022
The call() method allows function calls belonging to one object to be assigned and it is called for a different object. It provides a new value of this to the function. The call() method allows you to write a method once and allows it for inheritance in another object, without rewriting the method for the new object.
Syntax:
myFunc.call([thisArg[, arg1, arg2, ...argN]])
Parameters:
- thisArg: The values to use as this when calling myFunc function.
- arg1, arg2, ..., argN: These are the arguments for the above mentioned function.
Note: In certain cases, thisArg may not be the actual value. If the method is in non-strict mode, primitive values will be converted to objects and null,undefined will be replaced with the global object.
Return value: It returns the specified this value and arguments as a result of calling of the function.
Example 1: The following example demonstrates the use of calling to chain constructors for an object.
html
<script>
function Product(name,
price) {
this.name = name;
this.price = price;
}
function Vehicle(name,
price) {
Product.call(this,
name, price);
this.category = 'vehicle';
}
function Restaurant(name,
price) {
Product.call(this,
name, price);
this.category = 'restaurant';
}
const car = new Vehicle('Suzuki',
100000);
const restau = new
Restaurant('KFC', 1000);
console.log(car);
console.log(restaurant);
</script>
Output:
Example 2: The following example demonstrates the use of call() method to invoke an anonymous function.
html
<script>
const Birds = [
{ species: 'Pigeon', name: 'King' },
{ species: 'Crow', name: 'Fail' }
];
let i=0;
while(i<Birds.length){
(function(i) {
this.print = function() {
console.log('#' + i + ' '
+ this.species
+ ': ' + this.name);
}
this.print();
}).call(Birds[i], i);
++i;
}
</script>
Output:
#0 Pigeon: King
#1 Crow: Fail
Example 3: The following example demonstrates the use of call method to invoke a function and specifying the context for 'this'.
html
<script>
function greet() {
const reply = [this.animal,
'typically sleep between',
this.sleepDuration].join(' ');
console.log(reply);
}
const obj = {
animal: 'Rats',
sleepDuration: '2 and 5 hours'
};
greet.call(obj);
</script>
Output:
Rats typically sleep between 2 and 5 hours
Example 4: The following example demonstrates the use of call() method to invoke a function without specifying the first argument.
html
<script>
var str = 'Brad';
function display() {
console.log('string value is %s ',
this.str);
}
display.call();
</script>
Output:
string value is Brad
Note: In strict mode, the value of this will be undefined.
html
<script>
'use strict';
var str = 'Brad';
function display() {
console.log('str value is %s ',
this.str);
}
display.call();
</script>
Output:
Cannot read property 'str' of undefined
Similar Reads
JavaScript Function.prototype.bind() Method A function is a set of statements that take inputs, do some specific computation, and produce output. There are various scenarios in programming in which we need to pre-configure this keyword or function arguments and we can easily do that in JavaScript with the help of the bind() method. The bind()
2 min read
JavaScript function caller Property The function.caller property of the JavaScript function object returns the function that invoked the specified function. It returns null if the function "f" was invoked by the top-level code in JavaScript. For functions like strict functions, async functions, and generator functions this method retu
2 min read
JavaScript Function name Property The function name property of the javascript object is used to return the name of the function. This name property of the function is only readable and cannot be altered. The name of the function which was given when the function was created is returned by Function.name. Syntax: Function.name Proper
3 min read
JavaScript Function Call The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). This allows borrowing methods from other objects, executing them within a different context, overriding the default value, and passing arguments. Syntax: cal
2 min read
JavaScript Handler setPrototypeOf() Method JavaScript handler.setPrototypeOf() method in JavaScript is a trap for Object.setPrototypeOf() method and it returns a Boolean value. Syntax: const p = new Proxy(target, { setPrototypeOf: function(target, prototype) { } }); Parameters: This method accepts two parameters as mentioned above and descri
2 min read
JavaScript TypedArray.prototype.findIndex() Method Method TypedArray.prototype.findIndex() of JavaScript returns the index of the first element in the typed array that fulfils the conditions specified by the testing function, if no elements satisfy it, -1 is returned, the findIndex() method takes a callback function as an argument and it returns an
2 min read