This approach is very powerful. But until you understand how it works, it can seem rather mysterious and complicated. Fortunately, it's very straightforward to implement, so that's what we'll be looking at.

That "jQuery style" chainability is also known as a fluent interface. The fundamental thing that makes a fluent interface possible is for each method to return an object so that you can then call methods upon it.

The trick here is in our prototype methods. We created setName and setAuthor which can be called on any Book object. But then we made sure that when the methods had done their work, they returned the object they were originally called on.

Since that object was a Book, we could immediately call any Book method upon it. And that's all that chaining requires.

Thanks for reading!

Joshua Clanton

Want to improve your JavaScript skills?
Subscribe to A Drip of JavaScript for biweekly tips to help you level up as a JS developer.