统计数据未定义

Stats not defined

无法弄清楚为什么我收到统计信息未定义的错误...

我已经尝试命名和重命名并调用以前命名的函数仍然出现错误

    function GameObject(GO){     
    this.createdAt = GO.createdAt;     
    this.name = GO.name;    
    this.dimensions = GO.dimensions;    
   }

    GameObject.prototype.destroy = function(){    
    return `${this.name} was removed from the game.`;    
   }

    function CharacterStats(stats){    
    GameObject.call(stats);    
    this.healthPoints= stats.healthPoints    
   }

    CharacterStats.prototype = Object.create(GameObject.prototype)

    CharacterStats.prototype = takeDamage = function(){
    return `${this.name} took damage`;
   }
    function Humanoid(PettyHumans){
     this.team = PettyHumans.team;
     this.weapons = PettyHumans.weapons;
     this.language = PettyHumans.language;
     CharacterStats.call(this.PettyHumans)
    }

    Humanoid.prototype.greet = function(){
     return `${this.name} offers a greeting in ${this.language}.`;
    }

此代码将 undefined 传递给 CharacterStats:

function Humanoid(PettyHumans){
 this.team = PettyHumans.team;
 this.weapons = PettyHumans.weapons;
 this.language = PettyHumans.language;
 CharacterStats.call(this.PettyHumans)
 //                  ^^^^-------------- The problem is here
}

假设您通过 new Humanoid 调用 Humanoid,您还没有在任何地方设置 属性。 PettyHumans 是一个参数,而不是 属性。将其设置为 属性,或从该行中删除 this.


旁注:在一些地方,您似乎正在尝试实现 class-like 继承(例如,在 CharacterStatsGameObject 之间),但是代码正在执行那是不相当正确的。请参阅 (我也写过)了解如何更正它,继续使用 ES5 语法或使用 ES2015+ class 语法。但简而言之,使其正确的最小更改是添加一行:

GameObject.call(stats);    
    this.healthPoints = stats.healthPoints    
}

CharacterStats.prototype = Object.create(GameObject.prototype)
CharacterStats.prototype.constructor = CharacterStats  // Add this (and similar when doing other inheritance)

或者,再次使用 class。 (如果你需要支持 ES5-only JavaScript 引擎,你可以转译。)