Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript (2015) Constructor Functions and Prototypes Adding a Prototype

Can someone help me?

Is anyone working today? Can you please help me?

monster.js
function Monster(name) {
  this.name = name;
  this.health = 100;
  Monster.prototype{
    this.health--;
  }
}

When I did add a function, it said that Monster.prototype was not a function; what is the answer to this?

2 Answers

shilohpalomino
PLUS
shilohpalomino
Courses Plus Student 863 Points

Monster.prototype.What? You need to name the property/method you're adding to the prototype. Most likely damage. Is that your question?

Your code needs to look like this:

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

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

what you are doing is taking the takeDamage function out of the Monster constructor function and applying the prototype. The reason for this is so that once the constructor function is called the takeDamage function won't be called multiple times when you create objects based on the constructor function.

the same questions was asked on Treehouse earlier. I think Samuel's post is very useful.

https://teamtreehouse.com/forum/objectoriented-javascript