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 trialbhtlujtvuv
9,969 Pointsobject-oriented javascript
Challenge Task 1 of 1
We're adding a new method to every instance when a Monster is created. Use a prototype instead for the takeDamage method.
Any suggestions?
1 Answer
Samuel Webb
25,370 PointsHe wants you to remove the takeDamage()
function from the Monster()
constructor function and put in its own prototype. The reason for this is so that the function won't be created multiple times when you create objects based on the constructor function. Your code should look like this:
function Monster(name) {
this.name = name;
this.health = 100;
}
Monster.prototype.takeDamage = function(){
this.health--;
}
With takeDamage()
being a prototype, each object created from Monster()
will only reference the prototype instead of creating the method multiple times. With larger projects this will help the code run much faster.