September 2017
Intermediate to advanced
216 pages
6h 8m
English
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 = () => {}) => (action, ...args) => behaviors.get(action, defaultBehavior)(...args);
The behavior() higher-order function returns a new function that can be called with a given action and arguments to forward to the behavior. The behaviors argument is a map used to look up behavior ...
Read now
Unlock full access