其他分享
首页 > 其他分享> > JS compose 函数实现

JS compose 函数实现

作者:互联网

function compose(...funcs) {
    let length = funcs.length;
    if (length === 0) {
        return (arg) => arg;
    }
    if (length === 1) {
        return funcs[0];
    }
    return funcs.reduce(
        (a, b) =>
            (...args) =>
                a(b(...args))
    );
}
const fn1 = (x) => x + 10;
const fn2 = (x) => x * 10;
const fn3 = (x) => x - 10;
let result = compose(fn3, fn2, fn1)(1);
console.log(result);

参考链接 https://blog.csdn.net/MRlaochen/article/details/120437542

标签:funcs,...,compose,const,函数,JS,length,return
来源: https://www.cnblogs.com/beileixinqing/p/16611509.html