I want to display a message in the "toString" method about the status of a character true or false, depending on its health

function Character() { this.health = 100; this.basicDamage = 10; this.toString = function () { return this.name + this.status; // the properties that I want to reflect } } Character.createArcher = function () { var hero = new Character; hero.name = "Legolas"; // set hero name return hero; }; Character.isAlive = function () { var hp = new Character; if (this.health > 0){ hp.status = true; }else if (this.health <= 0){ hp.status = false; }else { alert("ERROR"); } return hp; }; var archer = new Character.createArcher(); console.log(archer.toString()); 

  • that's right, you create the var hero variable instead of this.hero in the local scoop, and accordingly toString gives void I think the same problem will be in the isAlive - Sultanov Shamil method

1 answer 1

The newly created character status property is not initialized. Rather, the object simply does not have this property.

 function Character() { this.status = "just_born"; this.health = 100; ...