首页 > 其他 > 详细

函数实现 composeFunctions(fn1,fn2,fn3,fn4)等价于fn4(fn3(fn2(fn1))

时间:2020-02-21 01:56:28      阅读:166      评论:0      收藏:0      [点我收藏+]
  • 函数组合运行
  • 说明:实现一个方法,可将多个函数方法按从左到右的方式组合运行。
  • composeFunctions(fn1,fn2,fn3,fn4)等价于fn4(fn3(fn2(fn1))
  •  1 const add = x => x + 1;
     2 const multiply = (x, y) => x * y;
     3 const multiplyAdd = composeFunctions(multiply, add);
     4 
     5 function composeFunctions() {
     6     var args = Array.prototype.slice.apply(arguments);
     7     console.log(‘args‘,args)
     8     return function() {
     9         if (args.length == 1) {
    10             console.log(‘args=1‘,this)
    11             return args[0].apply(this, Array.prototype.slice.apply(arguments));
    12 
    13         }
    14         console.log(‘args!=1‘,args)
    15         return composeFunctions(...args.slice(1))(args[0].apply(this, Array.prototype.slice.apply(arguments)));
    16     }
    17 }
    18 console.log(multiplyAdd(3, 4));// 返回 13

     

函数实现 composeFunctions(fn1,fn2,fn3,fn4)等价于fn4(fn3(fn2(fn1))

原文:https://www.cnblogs.com/xfcao/p/12339655.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!