【发布时间】:2020-05-04 20:08:22
【问题描述】:
我不确定为什么这不起作用,逻辑对我来说看起来不错。我希望更有经验的眼睛可以提供一些建议。这应该将_grit 返回为1。但它返回0。如果我创建gainedToughness(8),_grit 返回4,它应该返回3。
class Person {
constructor(name, age, profession){
this._name = name;
this._age = age;
this._profession = profession;
this._health = 100;
this._grit = 0;
}
get name() {
return this._name;
}
get age() {
return this._age;
}
get profession() {
return this._profession;
}
gotInjured(damageNum){
const weaker = this._health -= damageNum;
return weaker;
}
gainedToughness(damageNum){. <---------------------------
let grittier = this._grit++;
let lotGrittier = this._grit += 3;
return damageNum <= 5 ? grittier : lotGrittier;
}
}
const donRickles = new Person('donRickles', 33, 'comedian');
console.log(donRickles.gotInjured(1));
console.log(donRickles.gainedToughness(1)); <-----------
【问题讨论】:
-
++应该是+ 1,+=应该是+。
标签: javascript class methods