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 didn't he just do this?

var dice = new Dice(6);
var dice = new Dice(10);

instead of

var dice = new Dice(6);
var dice10 = new Dice(10);

plus all the rest of the code that was necessary to make those two lines equal, which I also don't get the point of. Basically the entire second half of this video didn't make any sense to me because I just don't get WHY we are doing it. Help please.

1 Answer

Erik Nuber
Erik Nuber
20,629 Points

In the video, he has created two sets of die. Each die is different, there is a six sided die and a ten sided die.

If we simply use the same variable dice to create a new version of each die than the second call would overwrite the first and we would be left with just a ten sided die

so he did the second way because they are two different instances of Dice.

var dice does not equal var dice10. That is why you send the function a number because it sets what sided die is being created.

assuming the area you are having problems with are prototypes which are not easy to understand, here is the info from when I asked about it months back.

This video below helped me understand prototype. https://youtu.be/pu08qQCmw8I

After watching the youtube video, I followed the link to his other lessons and watched the three on Object Oriented Programming. If you are reading this question and, find yourself lost, I would definitely recommend watching the three videos, the one above and two others that do a great job explaining the material.

https://www.developphp.com/video/JavaScript/Class-OOP-Tutorial-Intro-to-Object-Oriented-Programming