最近在学习es6-函数的扩展时,其中有介绍到部署管道机制时,用到了这段代码:

        const pipeline = (...funcs) =>
            val => funcs.reduce((a, b) => b(a), val);

        const plus1 = a => a + 1;
        const mult2 = a => a * 2;
        const addThenMult = pipeline(plus1, mult2);

        console.log(addThenMult(5))  //12;

刚看到这段代码,确实有点懵逼,小白就用es5的形式写了出来,code如下:

        var pipeline = function (...funcs) {
            return function (val) {
                return func.reduce(function (a,b) {
                    return b(a)
                },val)
            }
        }
        var plus1 = function (a) {
            return a + 1;
        }
        var mult2 = function (a) {
            return a * 2;
        }
        var addThenMult = function (param) {
            return pipeline(plus1,mult2)
        }
        console.log(addThenMult(5)) //结果如下;

然而打印出来确实pipeline方法返回的函数:

        function (val) {
            return func.reduce(function (a,b) {
                return b(a)
            },val)
        }

求大神指点,小白用es5改写后哪里出错了?


xibei
433 声望15 粉丝

当我闭上眼看世界时,才发现我已经落后许多啦·······