热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:函数实现composeFunctions(fn1,fn2,fn3,fn4)等价于fn4(fn3(fn2(fn1))

篇首语:本文由编程笔记#小编为大家整理,主要介绍了函数实现composeFunctions(fn1,fn2,fn3,fn4)等价于fn4(fn3(fn2(fn1))相关的知识,希望对你有一定的

篇首语:本文由编程笔记#小编为大家整理,主要介绍了函数实现 composeFunctions(fn1,fn2,fn3,fn4)等价于fn4(fn3(fn2(fn1))相关的知识,希望对你有一定的参考价值。




  • 函数组合运行

  • 说明:实现一个方法,可将多个函数方法按从左到右的方式组合运行。

  • 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

     




推荐阅读
author-avatar
静雨2502874293
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有