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 createTokens() Method Solution

Note: Corrected version of Token constructor see here. Question about When creating a new Token obj. (i, this),

First off there is a correction to the code: See Token.js, constructor and add params:

    constructor(index, owner) {

My question, when creating a new Token obj in Player.js

let token = new Token(i, this);

Why not be more specific and say (i, this.id) ?

The reason the whole owner object has to be passed as a parameter is because the Token class constructor stores a reference to that whole object, not just the id:

class Token {
  constructor(index, owner) {
    this.owner = owner; // here
    this.id = `token-${index}-${owner.id}`; // access individual properties of 'owner' as needed
    this.dropped = false;
  }
};

If Token didn't store an owner parameter, indeed, you could write:

class Token {
  constructor(index, owner_id) {
    this.id = `token-${index}-${owner_id}`;
    this.dropped = false;
  }
};

And call it with new Token(i, this.id).

Hope this helps!