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

Why does the new Token in Player accepts 2 arguments, 'i' and 'this', if the token constructor has no parameters?

The Token constructor in the class Token has no parameters:

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

But within the for-loop in the class Player, the statement that creates a new Token pass two arguments:

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

The reason was not clear to me. Shouldn't it return an error in this case or shouldn't we rather specify the constructor with these two parameters?

1 Answer

Steven Parker
Steven Parker
231,236 Points

At the bottom of the Token Properties Solution video page, in the "Teacher's Notes" tab, there is a "Code Correction" with a revised constructor definition for the Token object. It includes this line:

    constructor(index, owner){

And without the fix, I would expect to get an error in the console like this: "ReferenceError: owner is not defined".

Thank you Steven, I should have checked that. This helps me not to forget to do that.

Regards

Murilo