码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-02-20 23:37:49      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:console   turn   cti   func   argument   说明   ice   prototype   ret   

  • 函数组合运行
  • 说明:实现一个方法,可将多个函数方法按从左到右的方式组合运行。
  • 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))

标签:console   turn   cti   func   argument   说明   ice   prototype   ret   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!