The Javascript Function call is a predefined javascript method, that is used to write methods for different objects. It calls the method, taking the owner object as an argument. The keyword this refers to the “owner” of the function or the object it belongs to. All the functions in javascript are considered object methods. So we can bind a function to a particular object by using ‘call()’. A function will be the global object if the function is not considered a method of a JavaScript object.
Syntax:
call()
Return Value: It calls and returns a method with the owner object being the argument.
Example 1: Here is a basic example to describe the use of the call() method.
Javascript
function product(a, b) {
return a * b;
}
let result = product.call( this , 20, 5);
console.log(result);
|
Output:
100
Example 2: This example describes the use of function calls with arguments.
Javascript
let employee = {
details: function (designation, experience) {
return this .name
+ " "
+ this .id
+ designation
+ experience;
}
}
let emp1 = {
name: "A" ,
id: "123" ,
}
let emp2 = {
name: "B" ,
id: "456" ,
}
let x = employee.details.call(emp2, "Manager" , "4 years" );
console.log(x);
|
Output:
B 456
Manager
4 years
Example 3: This example describes binding a function to an object.
Javascript
let obj = { a: 12, b: 13 };
function sum() {
return this .a + this .b;
}
console.log(sum.call(obj));
|
Output:
25
We have a complete list of Javascript Functions, to check those please go through the Javascript Function Complete reference article.
Supported Browser:
- Google Chrome 1.0
- Firefox 1.0
- Microsoft Edge 12.0
- Internet Explorer 5.5
- Opera 4.0
- Safari 1.0
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
30 May, 2023
Like Article
Save Article