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 trialNicolas Filzi
18,021 PointsWhy is "this" in "this.sides" necessary in the randomNumber construction this time?
Thanks in advance for your answer folks :-)
Andrew Robinson
16,372 PointsI've never scrolled down far enough to see the "Add an Answer" box, glad it made sense though!
2 Answers
Andrew Robinson
16,372 PointsTechnically it's not needed because 'sides' is a parameter in the constructor and the roll method has access to that via closure.
Closure (if you don't already know) is when a local variable/parameter lives on even after the function has run, so in this case when the constructor is run, that instance of the sides parameter is still accessible because the roll method and this.sides are using it.
I will say though it is a best practice to access it with this. in this case, because if you update dice.sides in the future (for any reason, but this applies to other objects too) the method will point to this.sides, where as if you didn't use this.sides and just said 'sides', it would be accessing the parameter, not the property of the object.
Hope that makes sense, any questions just ask :)
Iain Simmons
Treehouse Moderator 32,305 PointsChanged to an answer! :)
LaVaughn Haynes
12,397 PointsI could give you a better example if I had access to your code, but basically, you are going to create a "template" object. Then you create one or many new instances of that template object to use in your program. "this" and "this.property" is a way to refer to the specific instance that you create.
//This is your "template" or "constructor"
var Person = function(name){
this.name = name;
};
Now you can make a bunch of instances based on that template
var mike = new Person('Mike Jones');
var joe = new Person('Joey Roberts');
var dana = new Person('Dana Carvey');
var chris = new Person('Chris Rock');
var karen = new Person('Karen Jordan');
All of these are based on the same template, but I know that when I print mike.name I'm not going to get the name property for joe because when we created mike we set his name property with this.name or "this instances name value" (which was set to "Mike Jones"). It's confusing at first but becomes very clear what's happening the more you practice.
Nicolas Filzi
18,021 PointsThanks for the thorough explanation of the constructor concept, though it was not exactly the point of my question :-)
LaVaughn Haynes
12,397 PointsHa! No problem. My membership is paused so I can't see the lesson you are on. I just thought I'd take a shot at what I thought you were asking.
Nicolas Filzi
18,021 PointsNicolas Filzi
18,021 PointsIt made perfect sense, thanks Andrew! You should have posted it as an answer to get the "Best Answer" Reward though ;-)