js currying & js 科里化
var test = (
function (a){
console.log(`a2 =`, a);// 1
// console.log(`b2 =`, b);
// undefined
this.a = a;
console.log(`this.a1 =`, this.a);// 1
return function (b) {
console.log(`this.a2 =`, this.a);// 1
console.log(`b3 =`, b);// 4
// ??? b always equal to test passed param's value
// passed & unused args/params
return this.a + b;
};
}(
function (a, b) {
console.log(`a1 =`, a);// 1
console.log(`b1 =`, b);// 2
return a;
}(1, 2)
// fixed args
)
);
console.log(`test(4) = `, test(4));
// a1 = 1
// b1 = 2
// a2 = 1
// this.a1 = 1
// this.a2 = 1
// b3 = 4
// test(4) = 5
// js curry & js 科里化
console.log(`
test(5) = `, test(5));
// b3 = 5
// test(5) = 6
柯里化
所谓"柯里化",就是把一个多参数的函数,转化为单参数函数;
https://www.ruanyifeng.com/blog/2017/02/fp-tutorial.html
柯里化(Currying), 又称部分求值(Partial Evaluation),是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数而且返回结果的新函数的技术;
https://www.zhangxinxu.com/wordpress/2013/02/js-currying/
柯里化有3个常见作用:
- 参数复用
- 提前返回
- 延迟计算/运行;
https://www.haorooms.com/post/js_currying
http://www.alloyteam.com/2013/08/javascript-zhong-you-qu-di-fan-ke-li-hua-ji-shu/
https://llh911001.gitbooks.io/mostly-adequate-guide-chinese/content/ch4.html
https://juejin.im/post/5af13664f265da0ba266efcf
js in deep
https://github.com/mqyqingfeng/Blog
https://blog.fundebug.com/2018/10/30/33-js-concepts/
refs
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!