Method Chaining in JavaScript
Last Updated :
15 Dec, 2023
As a good programming practice, we should write individual functions/methods for dealing with individual actions. And, writing only one method/function for all actions is a thing. However, sticking to good practice takes a toll on the readability and comprehensibility of the code, because defining a separate function for each action means that the output of a single function/method is input. This makes code comprehensibility even worse, as the function needs to be nested in reverse order. This is where method chaining comes to the rescue.
What is Method chaining?
It is a programming strategy that simplifies and embellishes your code. It is a mechanism for calling a method on another method of the same object.
JavaScript this keyword refers to the current object in which it is called. Thus, when a method returns this, it simply returns an instance of the object in which it is returned. Since the returned value is an instance of an object, it is, therefore, possible to call another method of an object to the returned value, which is its instance. This makes method chaining possible in JavaScript.
Example 1: In this example, each method in Land.prototype returns this, which refers to the entire instance of that Land object. This would help in calling a method on another method of the same object.
JavaScript
//creating a function Land()
function Land() {
this.area = '';
this.status = 'for Sale';
}
//Setting status open for sale
Land.prototype.open = function () {
this.status = 'Open for Sale';
return this;
}
//Setting status not for sale
Land.prototype.close = function () {
this.status = 'Not for Sale';
return this;
}
//Setting Parameters
Land.prototype.setParams = function (area) {
this.area = area;
return this;
}
//printing land status
Land.prototype.doorStatus = function () {
console.log('The', this.area, 'Land is', this.status);
return this;
}
//creating a land object
let land = new Land();
land.setParams("500 sq ft")
.close()
.doorStatus()
.open()
.doorStatus();
Output:
The 500 sq ft Land is Not for Sale
VM1106:24 The 500 sq ft Land is Open for Sale
Example 2: In this example, we will chain the inbuilt methods of the String
JavaScript
let firstName = " Rajat ";
console.log(firstName);
let modifiedName = firstName.toUpperCase()
.trim();
console.log(modifiedName)
Output:
Rajat
RAJAT
Similar Reads
JavaScript Array find() Method The find() method in JavaScript looks through an array and returns the first item that meets a specific condition you provide. If no item matches, it returns undefined. It skips any empty space in the array and doesnât alter the original array.Syntax:array.find(function(currentValue, index, arr), th
3 min read
JavaScript Handler apply() Method JavaScript handler.apply() method in JavaScript is used as a trap for a function call. The value returned by this method is used as the result of a function call through a proxy. Syntax: const p = new Proxy(target, { apply: function(target, thisArg, argumentsList) { } }); Parameters: This method acc
2 min read
JavaScript Array Iteration Methods JavaScript Array iteration methods perform some operation on each element of an array. Array iteration means accessing each element of an array. There are some examples of Array iteration methods are given below: Using Array forEach() MethodUsing Array some() MethodUsing Array map() MethodMethod 1:
3 min read
JavaScript Helper Methods An array in JavaScript is usually considered a "list-objects". In simple words, we can say an array is an object that contains some values. But an array is a special object in JavaScript. An array can store heterogeneous data structures. It can store data values of any type like objects and array. E
15+ min read
JavaScript Int8Array from() Method The Javascript Int8Array represents an array of twos-complement of 8-bit signed integers. By default, the contents of Int8Array are initialized to 0. from the () function of Int8Array used to create a new Int8Array from an array-like or iterable object. So when you want to convert an arrayLike or it
2 min read
JavaScript Handler construct() Method JavaScript handler.construct() method in JavaScript is a trap for the new operation and this method returns an object. Syntax: const p = new Proxy(target, { construct: function(target, argumentsList, newTarget) { } }); Parameters: This method accepts three parameters as mentioned above and described
2 min read