Cannot figure out why I am getting the error that stats is not defined when it is...
I have tried naming and renaming and calling the previously named function still getting the error
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}.`;
}
This code passes undefined
to CharacterStats
:
function Humanoid(PettyHumans){
this.team = PettyHumans.team;
this.weapons = PettyHumans.weapons;
this.language = PettyHumans.language;
CharacterStats.call(this.PettyHumans)
// ^^^^-------------- The problem is here
}
Assuming you call Humanoid
via new Humanoid
, you haven't set that property anywhere. PettyHumans
is a parameter, not a property. Either set it as a property, or remove the this.
from that line.
Side note: In a couple of places it looks like you're trying to implement class-like inheritance (for instance, between CharacterStats
and GameObject
), but the code doing that isn't quite correct. See this answer (which I also wrote) for how to correct it, either continuing with ES5 syntax or using ES2015+ class
syntax. But briefly, the minimal change to make it correct is to add a line:
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)
Or, again, use class
. (You can transpile if you need to support ES5-only JavaScript engines.)