这是我的代码
const Steps= {
ItemValue: 0,
GetNumber() {
return this.ItemValue;
},
Step(){
return this.getNumber()++;
},
StepBack() {
return this.getNumber()--;
},
NoStep(){
return this.getNumber();
}
}
我需要做这样的事情:这是一个问题的代码
const obj = {...Steps};
obj.Step()
.Step()
.StepBack()
.Step()
.StepBack()
.NoStep();
在C#中很容易,但是在这里我不能低估,问题出在哪里。您可以给我建议,如何用谷歌搜索或做出决定。
要在对象上链接方法,需要在每个方法中返回该对象,以便在该对象上调用下一个调用的方法。这this.getNumber()++
也将导致SyntaxError
。
const Steps = {
ItemValue: 0,
GetStep() {
return this.ItemValue;
},
Step() {
this.ItemValue++;
return this;
},
StepBack() {
this.ItemValue--;
return this;
},
NoStep() {
return this;
}
}
const obj = {
...Steps
};
obj.Step()
.Step()
.StepBack()
.Step()
.StepBack()
.NoStep()
console.log(obj.GetStep())
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句