近来在进修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改写后那里出错了?