Skip to content

Latest commit

 

History

History
29 lines (21 loc) · 603 Bytes

函数柯里化.md

File metadata and controls

29 lines (21 loc) · 603 Bytes

实现一个函数柯里化 pipe 方法, 传入任意个函数,上一个函数的返回值,是下一个函数的参数,依次执行

const result = pipe(fn1,fn2,fn3...)(params)

const pipe = (...args) => {
  const fns = Array.from(args);

  var bool = fns.every((fn) => toString.call(fn) === "[object Function]");

  if (!bool) {
    throw new Error("every params must be a function");
  }

  return function (e) {
    returnfns.reduce((pre, cur) => cur(pre), e);
  };
};

const fn = (a) => {
  returna + 1;
};

const fn2 = (a) => {
  returna + 1;
};

console.log(pipe(fn, fn2)(1));