Eloquent JS ch6虚拟猫练习 对象属性增减方法实现求助
代码现存问题
你的代码目前没有达到作业要求,核心问题如下:
- 属性修改逻辑完全未生效:所有方法内仅计算了临时变量
x用于打印日志,从未对this.tiredness/this.hunger等实例属性做重新赋值,无论调用多少次方法,四个属性的初始值永远为0,不存在实际的属性增减效果 - 方法命名不符合规则:题目明确要求方法名对应行为语义,你当前写的
energized/socialLife/feedMinus/isolated均为状态描述词,而非动作名,和题目给出的feed/sleep/pet的示例要求不符 - 属性增减逻辑颠倒:比如
feed(喂食)的效果应为降低饥饿度,你当前的逻辑是将传入值与饥饿度相加计算饱腹感;sleep(睡觉)应降低疲劳度,你写的「传入值减现有疲劳度」的计算逻辑完全不符合数值变化规律 - 遗漏核心需求:没有处理
loneliness(孤独度)的相关逻辑,也未实现题目要求的独立状态打印方法 - 缺少数值边界限制:后续接入CLI交互时,很容易出现属性为负、数值超过100%的不合理情况
修正后参考代码
const cat = { name: "Roy", // 所有属性取值范围限定为0-100,初始状态设为合理值 tiredness: 30, hunger: 30, loneliness: 30, happiness: 70, // 喂食:降低饥饿度,小幅提升快乐值 feed(amount = 15) { this.hunger = Math.max(0, this.hunger - amount); this.happiness = Math.min(100, this.happiness + 5); console.log(`你给${this.name}添了猫粮,它低头吭哧吭哧吃了起来`); }, // 睡觉:降低疲劳度,睡眠期间会小幅提升饥饿度 sleep(amount = 20) { this.tiredness = Math.max(0, this.tiredness - amount); this.hunger = Math.min(100, this.hunger + 8); console.log(`${this.name}蜷成毛团睡了一觉,醒了之后伸了个大大的懒腰`); }, // 抚摸:降低孤独度,提升快乐值 pet(amount = 12) { this.loneliness = Math.max(0, this.loneliness - amount); this.happiness = Math.min(100, this.happiness + amount); console.log(`你揉了揉${this.name}的软毛,它喉咙里发出了呼噜呼噜的声音`); }, // 陪玩:降低孤独度、提升快乐值,同时会增加疲劳度和饥饿度 play(amount = 15) { this.loneliness = Math.max(0, this.loneliness - amount); this.happiness = Math.min(100, this.happiness + amount); this.tiredness = Math.min(100, this.tiredness + 10); this.hunger = Math.min(100, this.hunger + 10); console.log(`你拿逗猫棒陪${this.name}玩了好一会,它上蹿下跳追得特别起劲`); }, // 时间流逝:模拟无操作时的自然属性递减/递增 passTime() { this.hunger = Math.min(100, this.hunger + 5); this.tiredness = Math.min(100, this.tiredness + 5); this.loneliness = Math.min(100, this.loneliness + 5); this.happiness = Math.max(0, this.happiness - 5); }, // 打印状态:按数值区间输出创意化描述 printStatus() { console.log(`\n=== ${this.name}的当前状态 ===`); // 饥饿度描述 if (this.hunger >= 80) console.log(`${this.name}快饿晕了,一直围着你转蹭你的腿要吃的`); else if (this.hunger >= 50) console.log(`${this.name}有点饿,时不时蹲在猫粮碗边看你`); else if (this.hunger >= 20) console.log(`${this.name}不怎么饿,正蹲在旁边慢悠悠舔爪子`); else console.log(`${this.name}吃得肚子圆滚滚的,瘫在地上摸起来软乎乎的`); // 疲劳度描述 if (this.tiredness >= 80) console.log(`${this.name}困得眼睛都睁不开,站着都能打盹`); else if (this.tiredness >= 50) console.log(`${this.name}有点累,趴在地上打了个小哈欠`); else if (this.tiredness >= 20) console.log(`${this.name}精神不错,正竖着耳朵听周围的动静`); else console.log(`${this.name}精力超级旺盛,满屋子跑酷根本停不下来`); // 孤独度描述 if (this.loneliness >= 80) console.log(`${this.name}独自待了太久,蔫蔫地蹲在角落不理人`); else if (this.loneliness >= 50) console.log(`${this.name}有点无聊,凑到你手边用脑袋蹭你求关注`); else if (this.loneliness >= 20) console.log(`${this.name}情绪平静,自己窝在猫爬架上看窗外的鸟`); else console.log(`${this.name}被陪得特别满足,一直黏在你脚边不肯走`); // 快乐值描述 if (this.happiness >= 80) console.log(`${this.name} is VERY happy!尾巴尖都晃出残影了`); else if (this.happiness >= 50) console.log(`${this.name}心情很不错,慢悠悠甩着尾巴看你`); else if (this.happiness >= 20) console.log(`${this.name}情绪平平,对什么都提不起太大兴致`); else console.log(`${this.name}很不开心,耳朵都背到脑袋后面了,哄不好的那种`); } } // 功能测试示例 cat.printStatus(); cat.feed(); cat.pet(); cat.play(); cat.printStatus(); cat.sleep(); cat.printStatus();
实现说明
- 所有属性值都通过
Math.max(0, 值)和Math.min(100, 值)做了边界兜底,永远不会出现负数或者超过100的不合理数值 - 所有交互方法都使用动作语义命名,每个方法带来的属性变化符合常识逻辑,不是生硬的数值加减
- 新增的
passTime方法专门用来实现你需要的属性自然递减效果:如果用户长时间不操作,每隔一段时间调用一次这个方法,就能模拟猫慢慢变饿、变累、变孤单、快乐值下降的效果 - 独立的
printStatus方法完全符合题目要求,不同数值区间对应不同的创意化表述,不是干巴巴打印百分比数字
内容的提问来源于stack exchange,提问作者HJ W
相关产品推荐
相关产品推荐

