In this chapter, we will learn about the call()
method in JavaScript. The call()
method is used to call a function with a given this
value and arguments provided individually. It allows you to invoke a function with a specific this
context, which can be useful in various scenarios. We will cover:
- What is the
call()
Method? - Syntax of the
call()
Method - Using
call()
to Invoke Functions - Borrowing Methods with
call()
- Using
call()
for Method Chaining - Simple Programs using
call()
What is the call() Method?
The call()
method is a built-in JavaScript function that allows you to call a function with a specified this
value and arguments. It is used for controlling the context in which a function is executed.
Syntax of the call() Method
Syntax
function.call(thisArg, arg1, arg2, ...);
thisArg
: The value to be used as thethis
value inside the function.arg1, arg2, ...
: Arguments to be passed to the function.
Using call() to Invoke Functions
The call()
method can be used to invoke functions and explicitly specify the this
context.
Example
function greet() { console.log(`Hello, my name is ${this.name}`); } let person = { name: "Ramesh" }; greet.call(person);
Output:
Hello, my name is Ramesh
Borrowing Methods with call()
You can use the call()
method to borrow methods from one object and use them on another object.
Example
let person1 = { name: "Rahul", age: 25, greet: function() { console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`); } }; let person2 = { name: "Neha", age: 30 }; person1.greet.call(person2);
Output:
Hello, my name is Neha and I am 30 years old.
Using call() for Method Chaining
The call()
method can be useful for method chaining, allowing you to call multiple methods in a sequence with a specific this
context.
Example
function setDetails(name, age) { this.name = name; this.age = age; } function introduce() { console.log(`My name is ${this.name} and I am ${this.age} years old.`); } let person = {}; setDetails.call(person, "Ramesh", 25); introduce.call(person);
Output:
My name is Ramesh and I am 25 years old.
Simple Programs using call()
Program 1: Calculating Area and Perimeter of a Rectangle
function Rectangle(width, height) { this.width = width; this.height = height; } function calculateArea() { return this.width * this.height; } function calculatePerimeter() { return 2 * (this.width + this.height); } let rect = new Rectangle(10, 5); let area = calculateArea.call(rect); let perimeter = calculatePerimeter.call(rect); console.log("Area:", area); console.log("Perimeter:", perimeter);
Output:
Area: 50 Perimeter: 30
Program 2: Using call()
to Sort an Array of Objects
let people = [ { name: "Ramesh", age: 25 }, { name: "Neha", age: 30 }, { name: "Rahul", age: 20 } ]; function compareAge(a, b) { return a.age - b.age; } people.sort(compareAge.call.bind(compareAge)); console.log(people);
Output:
[ { name: 'Rahul', age: 20 }, { name: 'Ramesh', age: 25 }, { name: 'Neha', age: 30 } ]
Conclusion
In this chapter, you learned about the call()
method in JavaScript, including its syntax and how to use it to invoke functions with a specific this
context, borrow methods, and chain methods. We also provided various use cases with simple programs to demonstrate the usage of the call()
method. Understanding how to effectively use the call()
method is essential for writing flexible and maintainable JavaScript code.