我想更改我的代码以执行链接。这是我的代码:
let stepCount = {
step: 0,
up() {
this.step++;
},
down() {
this.step--;
},
showStep: function () {
return this.step;
}
};
我应该如何更改它?我想执行此代码以获得答案
stepCount.up().up().down().up();
最佳答案
你只需要像这样从每个函数返回当前对象
let stepCount = {
step: 0,
up() {
this.step++;
return this;
},
down() {
this.step--;
return this
},
showStep() {
console.log(this.step)
return this;
}
};
stepCount.up().up().down().showStep().down().showStep();
关于javascript - 获取javascript代码并修改它以执行可链接的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71894221/