函数链式调用 (闭包)
// 依次执行f1、f2(f2使用f1的结果)
function f1(a) {
return a + 1;
}
function f2(a) {
return a + 1;
}
// function sum(...arr) {
// return arr.reduce((a, b) => {
// return (...args) => {
// return a(b(...args));
// };
// });
// }
// 简写
const sum=(...arr)=>arr.reduce((a,b)=>(...args)=>a(b(...args)))
sum(f1, f2)(1)
对象的链式调用
const proLink = {
name: "xixi",
age: 1,
add(value) {
this.age = this.age + value;
console.log("add", this.age);
return this;
},
des(value) {
this.age = this.age - value;
console.log("des", this.age);
return this;
},
};
console.log(proLink.name);
proLink.add(5).des(2);