How Does  Function Chaining Work in JQuery?

How Does Function Chaining Work in JQuery?

JavaScript is all about manipulating the DOM and, to this end, it needs to access the elements in the first place. Finding elements in Dom is not a cheap operation. Therefore there is a basic rule here which says: "Keep DOM access to a minimum."(1)

The best practice to access a DOM element in JavaScript is to save the element reference in a variable. In this way, JavaScript won't search the whole DOM tree to find the element on every attempt to manipulate it.

Bad: 

Document.getElementById("user-image").height = 200px;

Document.getElementById("user-image").width = 200px;

Good:

const userImage = Document.getElementById("user-image");

userImage.height = 200px;

userImage.width = 200px;

Now, there is another drawback when manipulating DOM elements. Whenever we change anything in the DOM tree, the browser syncs the UI accordingly.

"This can sometimes consume a lot of CPU & memory because changing the layout of one element may affect the layout of many other elements on the screen."(2)

In the above example, the browser have to recalculate and repaint the adjacent elements twice: once with resetting the width and once with resetting the height.

Function chaining in JQuery is a solution for this issue.

No alt text provided for this image


Function Chaining

"Function chaining is a pattern in JavaScript where multiple functions are called on the same object consecutively. Using the same object reference, multiple functions can be invoked. It increases the readability of the code and means less redundancy."(3)

Using function chaining in JQuery saves us from attaching the element reference multiple times for each function call. In the example above, we can rewrite our code as follows to make it more efficient and more concise at the same time:

var userImage = $("#user-image")

userImage.height("200px").width("200px")

But how does function chaining work? Normally when we change a specific property of an element, we only reset the value of a property in an object.

In function chaining the same job is done through a function which returns the object reference which can be used by the next function. Therefore, the browser doesn't need to access the element twice. The following example shows the same effect and gives us a better understanding of how this patterns works under the hood.

var Element = {

  height: 0;

  width: 0;

  height: function(height) {

    this.height = height;

    return this;

  }

  width: function(width) {

    this.width = width;

    return width;

  }

}


Element.height(100).width(100);

When we invoke height(100) on Element, it returns the same context(Element) to invoke the next function (here width) on it. In so doing, the browser will consume less memory and CPU to apply the changes on the same object. 


Conclusion

Making a program work is one thing, making it work efficiently is another thing. Using best practices and effective patterns is not an option in software development world; it is a must. This is what makes our products more enjoyable and easier to use for the users. When working with DOM, applying best practices such as function chaining has a huge impact on the whole application performance which leads to more user satisfaction.


Sources

1. JavaScript Best Practices

2. What is Virtual DOM and What Problem Does it Solve?

3. JavaScript Function Chaining

要查看或添加评论,请登录

Mojtaba F.的更多文章

  • Rest Parameters and Spread Syntax

    Rest Parameters and Spread Syntax

    In this article, I want to talk about the famous three-dots in JavaScript. Depending on the context it is used, it…

  • Array Loop in JavaScript

    Array Loop in JavaScript

    Arrays in JavaScript are a special kind of object. The main difference between an array and a regular object in…

  • How Closure works in JavaScript

    How Closure works in JavaScript

    So many talks about closures in JavaScript. If you are in the beginning stage of learning JavaScript, you may have a…

  • 3 Amazing New Features in ES2021

    3 Amazing New Features in ES2021

    EcmaScript specifications yearly update for 2021 is no slouch. Here I will introduce you with 3 highlight features…

社区洞察

其他会员也浏览了