代码拉取完成,页面将自动刷新
function add(a, b) {
return a + b;
}
function square(a) {
return a * a;
}
function plusOne(c) {
return c + 1;
}
// const pipe = (...args) => {
// return (...arguments) => {
// const init = args[0].apply(null, arguments);
// return args.slice(1).reduce((memo, current) => {
// return current(memo);
// }, init);
// };
// };
const pipe = (...args) => {
//接收多个函数
return (...arguments) => {
return args.reduce((memo, current) => {
//第一个参数是前一个执行的返回值
//第二个参数是当前的函数
//把当前函数的执行返回值给下一个函数
//reduce过程中不停的执行
return current(
typeof memo === "function" ? memo.apply(memo, arguments) : memo
);
});
};
};
const compose = (...args) => {
return (...arguments) => {
return args.reduceRight((memo, current) => {
return current(
typeof memo === "function" ? memo.apply(memo, arguments) : memo
);
});
};
};
var addSquareAndPlusOneCompose = compose(plusOne, square, add);
var addSquareAndPlusOne = pipe(add, square, plusOne);
// function composite(add, square, plusOne) {
// return function () {};
// }
// console.log(addSquareAndPlusOne(1, 2));
console.log(addSquareAndPlusOneCompose(1, 2));
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。