So far, we've been able to encapsulate mapped behaviors inside of a function. But in order to build complex applications, you need a means to compose larger behavior out of smaller behavior.
Composing behavior
A generic higher-order behavior function
You can enhance the approach we've been following so far with our mapped behavior to produce a higher-order function: a function that returns other functions. For example, we've been creating new Map instances every time we invoke behavior, which is unnecessary. If we return a new function, we don't have to store our mapped behavior anywhere—we just have to pass it in as an argument:
const behavior = (behaviors, defaultBehavior = () => {}) =>
...