品易云推流 关闭
文章详情页
文章 > JavaScript > js柯里化函数的好处

js柯里化函数的好处

js柯里化函数

头像

小妮浅浅

2021-08-12 09:19:572424浏览 · 0收藏 · 0评论

好处说明

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电脑。

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图