lodash 库 提供了组合函数的方法:

  • flow() 从左往右执行
  • flowRight() 从右往左执行,使用的更多一些
  1. // 使用lodash 的 组合函数
  2. const _ = require('lodash');
  3. // 求数组的最后一项,并转换成大写
  4. const first = arr => arr[0] ;
  5. const reverse = arr => arr.reverse();
  6. const toUpper = str => str.toUpperCase();
  7. const fnRight = _.flowRight(toUpper,first,reverse);
  8. const fn = _.flow(reverse , first, toUpper);
  9. console.log(fnRight(['one','two','three']));
  10. console.log(fn(['one','two','three']));