Providing method chaining
Chaining more than one method is a very useful feature when programming in jQuery. Chaining is possible since most jQuery methods return an object allowing the calling of other methods on the returned object. Since the child methods are executed in the returned object instead of the entire DOM, the code runs faster. This allows you to write code, which is not only shorter but also faster.
The plugin that we have worked with so far does not support chaining. In this example, let's modify the plugin to include this feature.
The programming constructs used in this example are summarized as follows:
Construct |
Type |
Description |
---|---|---|
|
jQuery function |
This refers to the jQuery function. |
|
jQuery selector |
This selects an element based on its |
|
jQuery object |
This refers to the current jQuery object. |
|
jQuery function |
This merges the contents of two or more objects into the first object. |
|