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

Don't quite understand the createTokens method.

In our Token.js file, we created a constructor that looks like this:

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

In the createTokens method in Player.js, there's a for loop that looks like this:

        for(let i = 0; i < num; i++) {
            let token = new Token(i, this);
            tokens.push(token);
        }

How are we able to pass arguments when creating a new Token in the for loop when we've not specified any parameters in the constructor of class Token? We're passing it the index of the loop as well as 'this' but I don't see how that's possible?

Thank you!

1 Answer

Steven Parker
Steven Parker
231,275 Points

There's a Course Correction in the Teacher's Notes section of the previous video.

The corrected code for the constructor method has this change:

    constructor(index, owner) {

Oh oops. That's what I get for trying to fly through everything quickly. Thank you Steven.