Chaining and fluent interfaces

When you work with objects or arrays, there is another way of linking the execution of several calls together, by applying chaining. For example, when you work with arrays, if you apply a .map() or .filter() method, the result is a new array, to which you can apply a new further method, and so forth. We have already used such methods, as when we defined the range() function back in the Working with ranges section of Chapter 5, Programming Declaratively - A Better Style:

const range = (start, stop) =>    new Array(stop - start).fill(0).map((v, i) => start + i);

First, we created a new array; then, we applied the .fill() method to it, which updated the array in place (side effect...) and returned the updated array, ...

Get Mastering Javascript Functional Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.