链式(调用)
有一个可以上下移动的 ladder 对象:
let ladder = {
step: 0,
up() {
this.step++;
},
down() {
this.step--;
},
showStep: function () { // 显示当前的 step
alert(this.step);
}
};
现在,如果我们要按顺序执行几次调用,可以这样做:
ladder.up();
ladder.up();
ladder.down();
ladder.showStep(); // 1
修改 up,down 和 showStep 的代码,让调用可以链接,就像这样:
ladder.up().up().down().showStep(); // 1
这种方法在 JavaScript 库中被广泛使用。
let ladder = {
step: 0,
up() {
this.step++;
return this;
},
down() {
this.step--;
return this;
},
showStep: function () { // 显示当前的 step
alert(this.step);
return this;
}
};
console.log(ladder.up().up().down().showStep())
js链式调用
最新推荐文章于 2023-09-13 12:40:17 发布