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 trialNathan Keene
7,595 Points`Monster.prototype.takeDamage` is not a function
Getting this whether I have the new property declared or not, even when the method is still on the constructor and the starter code is untouched. Code for the prototype addition:
Monster.prototype.takeDamage = function() { this.health--; };
var Monster = function(name) {
this.name = name;
this.health = 100;
this.takeDamage = function() {
this.health -= 1;
}
};
2 Answers
Bruno Navarrete
Full Stack JavaScript Techdegree Graduate 22,246 PointsThis code should be after your prototype declaration (var Monster...
):
Monster.prototype.takeDamage = function() {
this.health -= 1;
}
You'll also have to remove this.takeDamage
from the prototype.
Nathan Keene
7,595 PointsBruno, thanks. I swear that's exactly how I had it and it wasn't working before but now it is. I think I might have been leaving out "prototype" from the property assignment in my original code. Either that or it's those damn JavaScript gremlins! :-)
Anyway this works as it should:
function Monster( name ) {
this.name = name;
this.health = 100;
}
Monster.prototype.takeDamage = function() {
this.health--;
};