Использование вами ++
и +=
вызывало ваши проблемы. Поскольку они оба предназначены для переназначения переменной, а это не то, что вам нужно.
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 + 1;
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));
console.log(donRickles.gainedToughness(8));
Если вы хотите обновить значения своих свойств, а также вернуть новое значение, сделайте следующее:
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;
this._health = weaker;
return this._health;
}
gainedToughness(damageNum){
let grittier = this._grit + 1;
let lotGrittier = this._grit + 3;
this._grit = damageNum <= 5 ? grittier : lotGrittier;
return this._grit;
}
}
const donRickles = new Person('donRickles', 33, 'comedian');
console.log(donRickles._health)
console.log(donRickles.gotInjured(1));
console.log(donRickles._health)
console.log(donRickles._grit)
console.log(donRickles.gainedToughness(1));
console.log(donRickles._grit)