首先实现柯里化函数es6
function curry(fn) { const len = fn.length; return function curred(...args) { if (args.length >= len) { return fn(...args); } else { return curred.bind(null, ...args); } }; }
调用函数
function show(a = 0, b, c) { console.info(a, b, c); } const next = curry(show); next("g")("ccc")("ooo");
输出:spa
为何会这样?
去掉参数a的默认值就行了,查阅函数的扩展 ### 函数的 length 属性作了详细的说明:第一个参数a设置了默认值致使show.length为0,(也就是说默认值致使函数参数的长度变小了)这样传递一个参数"g"调用执行了show,show执行完返回的是undefined因此后面再调用next就报错了。因此对须要柯里化的函数尽可能不要传递默认值。code