lodash 中的函数组合的方法_.flow()与 _.flowRight()
_.flow([funcs])
创建一个函数。 返回的结果是调用提供函数的结果,this
会绑定到创建函数。 每一个连续调用,传入的参数都是前一个函数返回的结果。
_.flowRight()
类似 _.flow
,除了它调用函数的顺序是从右往左的。
Eg:
const reverse = arr => arr.reverse() // 倒叙
const first = arr => arr[0] // 选取第一个
const toUpper = s => s.toUpperCase() // 转化为大写字母
const f = _.flowRight(toUpper, first, reverse)
// flowRight 方法是从右往左执行的
console.log(f(['one', 'two', 'three']))
// 所以打印出来是 THREE