实现对象方法的链式调用
- 单一方法调用
let ladder = {
step: 0,
up() {
this.step++;
},
down() {
this.step--;
},
showStep: function() { // 显示当前的 step
console.log(this.step);
}
}
ladder.up(); // 1
ladder.down(); // 0
ladder.down(); // -1
ladder.showStep(); // -1
- 链式调用
let ladder = {
step: 0,
up() {
this.step++;
return this;
},
down() {
this.step--;
return this;
},
showStep: function() { // 显示当前的 step
console.log(this.step);
return this;
}
}
ladder.up().up().down().up().down().showStep(); // 1