【问题标题】:need help fixing a small method in a class需要帮助修复类中的一个小方法
【发布时间】: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));      <-----------


【问题讨论】:

标签: javascript class methods


【解决方案1】:

您对+++= 的使用导致了您的问题。因为它们都是为重新分配变量而设计的,这不是你想要的。

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)

【讨论】:

  • 已编辑以包含用于更新属性值并返回它们的选项。
  • 如果我想同时降低生命值但增加勇气,我是否必须在gotInjured 中添加另一个函数?我试图返回两个值,但不知道该怎么做。
  • 一般来说,您可以将两个值作为数组(编号列表)或对象(命名列表)返回,但也许我应该首先询问您为什么对返回值感兴趣。将逻辑分为 getter 和 setter 是很常见的,换句话说,一种方法将“设置”prop 的值,而另一种方法将“获取”或返回它。如果你想一次更新两个 props,你可以创建一个像 setAandB(a, b) 这样的方法,在其中调用 setA(a)setB(b)。要取回值,您可以调用像 getAandB() 这样的方法,它返回一个对象 {a: this.a, b: this.b}
猜你喜欢
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多