js函数柯里化

418 阅读1分钟
  • 判断当前函数传入的参数是否大于或等于函数执行所需要的最大参数的数量,如果是,直接执行函数
  • 如果传入参数数量不够,返回一个闭包,缓存之前传入的参数,并重新返回currying函数

es6实现

function currying(fn, ...params) {
  if (params.length >= fn.length) {
    return fn(...params);
  } else {
    return (...rest) => currying(fn, ...params, ...rest);
  }
}
// 定义一个toDo函数
// 假定需要满足a,b,c3个参数
function toDo (a, b, c) {
    console.log(a, b, c);
}
const curryingFn = currying(toDo);
example1: curryingFn('参数一')('参数二')('参数三'); // 参数一 参数二 参数三
example2: curryingFn('参数一', '参数二')('参数三'); // 参数一 参数二 参数三
example3: curryingFn('参数一', '参数二', '参数三'); // 参数一 参数二 参数三

example4: curryingFn('只传参数一') // 返回一个新的函数 (...rest) => currying(fn, ...params, ...rest);

es5实现

function currying (fn) {
    if (typeof fn !== 'function') {
        throw new Error('fn should be function')
    }
    const total = Array.prototype.slice.call(arguments);
    const params = Array.prototype.slice.call(arguments, 1);
    if (params.length >= fn.length) {
       return fn.apply(null, params);
    } else {
        return function () {
            let args = Array.prototype.slice.call(arguments);
            args = total.concat(args);
            return currying.apply(null, args);
        }
    }
}

function a (a, b, c) {
    console.log(a, b, c);
}
const curryFn = currying(a);

curryFn(1, 2, 3) // 1 2 3
curryFn(1)(2, 3) // 1 2 3
curryFn(1, 2)(3) // 1 2 3