function curry(fn){ // 代碼 } function add(a,b,c){ return a + b + c; } const execAdd = curry(add); execAdd(1)(2)(3) === 6; // true execAdd(1,2)(2) === 6; // true execAdd(1,2,3) === 6; // true function mulit(a,b,c,d){ return a * b * c * d; } const execMulit = curry(mulit); execMulit(1)(2)(3)(4) === 24; // true execMulit(1, 2)(3)(4) === 24; // true execMulit(1, 2, 3)(4) === 24; // true execMulit(1, 2, 3, 4) === 24; // true
這是2018年七牛雲前端校招春招的一道題目前端
function curry(fn){ let fnLent = fn.length, args = []; return function _curry(){ for (const arg of arguments) { args.push(arg) } return args.length === fnLent ? fn.apply(this,args) : _curry; } }
主要解題思路是fn.lengthapp