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 trialJonathan Grieve
Treehouse Moderator 91,253 PointsWhy index and not id as constructor parameter?
In the correction code in the teachers notes, I can't work out why we don't pass id
as the parameter instead of index
?
Shouldn't we be using id as that is the name of the property used in the constructor method, but index is used later when we later define a custom method.
class Token {
constructor(owner, index) {
this.owner = owner;
this.id = `token-${index}-$(owner.id}`;
this.dropped = false;
}
}
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Jonathan Grieve ! We do pass in the id
in a roundabout sort of way. We pass in the owner
. Each owner has their own id
. We then set the id
of the token to be a string that reflects the index
and the id of the owner.
Hope this helps!
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsSo what's happening here is the 2 classes, Player and Token are being linked by their constructors? And owner refers to the Player class? But I still don't see where there's any reference to the player class. Or does this come later? Having a hard time wrapping my head around this! :)
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherIn your Player object, you have an array representing that players tokens. That array will eventually hold instances of tokens which is what we're writing here. Remember, classes can hold instances of other classes and take instance of other classes and all sorts of things. Check out Ashley's last comments of the preceding video:
Later on down the road, we'll be creating instances of these tokens for the Player and storing them in that array. When we do, that's when we pass in the "owner" which at that point will likely be
this
because the player instance has an array which are instances of tokensJonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsThanks for the pointers. Think it's starting to make a little more sense. :) I guess it'll all become clearer as the course goes on. Useful to know there is a reason for the parameter choices. Really enjoying it so far!