admin 管理员组

文章数量: 1086871

柯里化~~

f(a,b,c) → f(a)(b)(c) // 部分函数调用 f(a,b,c) → f(a)(b,c) / f(a,b)(c)


// 没有保存调用时的this
function currying(fn, ...args) {if (args.length >= fn.length) {return fn(...args);} else {return (...args2) => currying(fn, ...args, ...args2);}}
// 保存下this
function curry(fn) {const ctx = this;function inner(...args) {if (args.length === fn.length) return fn.call(ctx, ...args);return (...innerArgs) => inner.call(ctx, ...args, ...innerArgs);}return inner;
}// test
function test(a, b, c) {console.log(a, b, c);
}
const f1 = curry(test)(1);
const f2 = f1(2);
f2(3);// test
function test(a, b, c) {console.log(a, b, c);
}const f1 = curry(test)(1,2);
// const f2 = f1(2);
f1(3);

本文标签: 柯里化