模拟实现
const slice = Array.prototype.slice; const curryMaker = function(fn, len) { const curriedArgs = slice.call(arguments, 2); return curry.apply(this, [fn, (len || fn.length), ...curriedArgs]); }; const curry = function(fn, len) { const curriedArgs = slice.call(arguments, 2); return function() { const args = [...curriedArgs, ...arguments]; // 如果与预期的参数长度一致,则执行函数,获取返回值 if (args.length >= len) { return fn.apply(this, args); } // 否则,整理入参,返回curry函数的结果 return curry.apply(this, [fn, len, ...args]); }; }; function calcSum() { return [...arguments].reduce((pre, value) => { return pre + value }, 0); } const calcSumCurry = curry(calcSum, 4); const nextCalc = calcSumCurry(1, 2); console.log(nextCalc(3)(4));
标签:...,return,len,JS,柯里化,const,随笔,curry,fn From: https://www.cnblogs.com/fanqshun/p/15967674.html