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: Challenge Building Constructor Methods and Generating Objects Token Properties Solution

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Why 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 idas 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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi 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! :sparkles:

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

So 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

In 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:

Most of the values for this properties will be passed in when the player objects are created. The exception to this is the tokens property. In a future step, we'll be writing a method inside this class that creates the token objects for us.

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 tokens :smiley:

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Thanks 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!