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 Prototypes Review

I'm drawing a blank on this prototype question

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

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

I tried "this.health" and "health". Both are incorrect, so idk.

Mark Miller
Mark Miller
45,831 Points

This is a prototype function. Every time you create a "new" Monster, it should be able to invoke this takeHit function. The special word prototype is used in the dot notation here in between Monster and takeHit, so that any object of the Monster type will be able to use this takeHit function. So this declaration needs the word prototype included.

Thank you Mark, I did prototypes on w3s, I don't remember them using .prototype syntax. Is this a long hand version of another syntax?

Kevin Faust
Kevin Faust
15,353 Points

scroll to the very bottom of w3s javascript prototypes and you will find it. its the most standard and common way to creating prototypes

Thanks kevin, I will take another look at w3s, I must have missed it.

1 Answer

Kevin Faust
Kevin Faust
15,353 Points

its "prototype" ^^ because all monsters should be able to be hit

Thank you kevin. Out of three questions "prototype" was the answer to two of them. "because all monsters should be able to be hit it", makes perfect sense.