Method Chaining in JavaScript
Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results. (From Wikipedia)
Chainable object design and chaining :
Chaining and Chainable is a design methodology used to design object behaviors so that calls to object functions return references to self, or another object, providing access to additional function calls allowing the calling statement to chain together many calls without the need to reference the variable holding the object/s.
Objects that can be chained are said to be chainable. We should be ensured that all returned object are of correct type if not then chain will fail.
Note: Do not use ambiguous function in chaining like .toString which returns string instead of return this.
Method Chaining
Method Chaining is the strategy of programming to make code more readable, clean and attractive. In this chaining, we return whole object instead of single element.
Example:
Note: Each method in Door.prototype returns this , which refers to the entire instance of that Door object.