• 技术文章 >Web开发 >JavaScript

    js柯里化函数的好处

    小妮浅浅小妮浅浅2021-08-12 09:19:57原创2002

    好处说明

    1、可以把函数式编程变得简洁,没有冗余。

    2、尽管有多个参数,仍然可以保留数学函数的定义。

    3、可以将函数作为返回值输出,提前返回。

    实例

    match(/r/g, 'hello world'); // [ 'r' ]
     
    const hasLetterR = match(/r/g); // x => x.match(/r/g)
    hasLetterR('hello world'); // [ 'r' ]
    hasLetterR('just j and s and t etc'); // null
    filter(hasLetterR, ['rock and roll', 'smooth jazz']); // ['rock and roll']
     
    const removeStringsWithoutRs = filter(hasLetterR); // xs => xs.filter(x => x.match(/r/g))
    removeStringsWithoutRs(['rock and roll', 'smooth jazz', 'drum circle']); // ['rock and roll', 'drum circle']
    const noVowels = replace(/[aeiou]/ig); // (r,x) => x.replace(/[aeiou]/ig, r)
    const censored = noVowels('*'); // x => x.replace(/[aeiou]/ig, '*')
    censored('Chocolate Rain'); // 'Ch*c*l*t* R**n'

    以上就是js柯里化函数的好处,希望对大家有所帮助。更多js学习指路:js教程

    推荐操作环境:windows7系统、jquery3.2.1版本,DELL G3电脑。

    专题推荐:js柯里化函数
    品易云
    上一篇:javascript如何扩展内置对象 下一篇:js中repeat()的使用

    相关文章推荐

    • js外观模式的介绍• js观察者模式是什么• js观察者模式和订阅模式的区别• js迭代器模式是什么• js状态模式是什么• javascript如何动态加载js文件• js中如何自定义迭代行为• js返回生成器的两种方法• js中yield和yield*表达式的介绍• js生成器中next的使用• throw在js生成器中的用法• js中return如何结束生成器• js中co模块是什么

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网