Function Chaining In JavaScript

Introduction

It is really easy to maintain small concise code in JavaScript. If there are multiple actions needed to be performed on an object, then the developer needs to write many lines of code to call the functions. In that case, it will be difficult for other developers to understand and maintain in the future. It would be really easy if you can call subsequent actions on an object one by one.

There are some jQuery function chainings available which perform the chaining task seamlessly.
  1. $("div").addClass("greenbackground").removeClass("className");   
Let's learn to create this chain function for a custom object in JavaScript.

Example
  1. var class1 = function() {  
  2.     this.result = 0;  
  3.     this.add = function(input) {  
  4.         this.result += input;  
  5.         return this;  
  6.     }  
  7.     this.substract = function(input) {  
  8.         this.result -= input;  
  9.         return this;  
  10.     }  
  11.     this.print = function() {  
  12.         console.log(this.result);  
  13.     }  
  14. };  
  15. var object = new class1();  
  16. object.add(3).substract(2).print();  
Explanations

In the above example, I am creating a constructor - "class1". It has a variable result which is assigned as 0 at the time of initialization. This constructor also contains 3 methods. 
  1. add - which adds the input to the existing value.
  2. substract - which subtracts the given input from the existing value.
  3. Print - It prints the values after the opertation.
Without Function chaining, if we need to do multiple operations, then we need to write multiple lines of code.
  1. object.add(3);  
  2. object.subtract(2);   
  3. object.print();   
For a simple opertaion, only 3 lines of code are needed here. To avoid that, chaining concept is implemented.

To provide function chaining, each function needs to return the object so that the next function can be implemented on the output of the first function, like in the above example.
  1. return this;  
is present in both add and subtract functions. But for printing, we don't need to add this line as it will always be the last function to be called in a function chain to print the output. By returning the object after the evaluation, the function provides a scope for chaining. Like here, add function is open for chaining by returning the object and same is with subtract function to implement fnction chaining developer needs to create object like,
  1. var object = new class1();  
It will ensure that the "object" will inherit all the properties of the constructor (class1 here). It ensures smooth functioning of function chaining. Function chaining can also be implemented with the closure in JavaScript

Conclusion

Function chaining is a nice concept provided in JavaScript. It encourages developers to create higly maintainable code. Please provide your feedback in the comments. This will encourge me to improve myself.
Ebook Download
View all
Learn
View all