Chaining in Javascript

sharu24

Sharu

Posted on January 21, 2023

Chaining in Javascript

Chaining is a means of simplifying the process to map, filter, extrapolate or reduce the data in a effective and user friendly manner.

Being widely utilized in Javascript libraries in the early days with Jquery and Underscore.js, its usage even in the present highly benefits many modern Javascript Libraries and Frameworks like React, Vue and Angular in making applications which are simple to design, keeping data abstract and code more modular.

Though the terminology might not be frequently used, one would have come across this to filter or map data sets. Backbone of Data Analytical and streaming applications is based out of this concept ( Eg: MapReduce in distributed file system processing)

One can build custom libraries based on its ease of implementing one using Javascript Objects. In fact all the inbuilt functions from Prototype Inheritance that Javascript Arrays and Objects provide is based out of Chaining. Example is as shown below

Snap a: Chaining process using Javascript Object

let chain = {
  result: 0,
  sum: function (param) {
    this.result = this.result + param;
    return this;
  },
  sub: function (param) {
    this.result = this.result - param;
    return this;
  },
  mul: function (param) {
    this.result = this.result * param;
    return this;
  },
  print: function () {
    console.log(`Result = ${this.result}`);
  },
};

chain.sum(5).mul(50).print();
Enter fullscreen mode Exit fullscreen mode
đź’– đź’Ş đź™… đźš©
sharu24
Sharu

Posted on January 21, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related