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 Methods with Prototypes

Why dice.roll and dice10.roll should be the same if they are passing a diferent argument? (sec. 26.)

Every instance pass a diferent argu to the constructor and when we do this "console.log(dice.roll === dice10.roll)" in sec. 26, he says that it should be true instead of false. This doesnΒ΄t make pretty much sence to me since wey are refering to diferent instance and passing diferent arguments to the constructor function. Help please!

4 Answers

Jeremy Castanza
Jeremy Castanza
12,081 Points

Think of it as comparing code versus the output of the executed code. Here's a quick thing that you can try to see what I'm saying.

In your console, try typing the following:

dice.roll

Now, try typing:

dice.roll()

Without parenthesis, you're basically just comparing a method built in a constructor and one that's built as part of a prototype. This is why it evaluates to FALSE in the beginning because he's trying to show that it's not the same method being called when the method isn't built as part of a prototype.

With parenthesis, you're actually executing the method itself which returns a value. I believe this is what you were thinking would happen when it did the comparison with the console log method.

Ultimately, Andrew is basically just comparing different pieces of code. He's not comparing the output of that code, which would return different values if a die with a parameter of six and a die with a parameter of ten was passed to the method.

Hope that helps!

amourief
amourief
5,469 Points

Great explanation Jeremy Castanza - it helped me too :)

Steven Parker
Steven Parker
229,695 Points

:point_right: Both instances share the same roll method code.

Since the method is implemented as a prototype, instead of each instance having it's own copy of the method code, all instances share the same code.

That's why if you compare the methods for equality, the result is true.

Great explanation! Thanks!

It's not that the two instances of Dice are being compared, but the prototype method itself. Of course, you can't say that dice === dice10 because they are different instances with different side properties. However, both instances of Dice, and any other instances use the exact same prototype regardless.

dice.roll === dice10.roll
Dice.prototype.roll === Dice.prototype.roll 

It's pretty much the same. Kind of redundant, but it shows that prototyping is a bit more efficient.

Thanks, this confused me.