New To Javascript Objects/prototypes

I’m trying to make something small and simple:

function Monster(rank) {
  this.rank = rank;
  this.health = 100;
}

Monster.prototype.takeHit = function() {
  this.health--;
}

how do I make the monster take a hit and show in the console log?

Monster.takeHit; //???

console.log(health); //???

You need instantiate it and call the function with ().

const monster = new Monster(1)
monster.takeHit()
console.log(monster.health)
1 Like

but when I try to have it take another hit, it gives me another error in the console log:
VM84:1 Uncaught SyntaxError: Identifier ‘monster’ has already been declared
at :1:1

why? Does it need to refresh somehow?

You only need to instantiate each monster once. You can just call the takeHit function afterwards.

1 Like

thanks for helping a newb whose strugglin!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.