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

valeria cravea
valeria cravea
3,986 Points

word this - function createTokens i dont understand what this is referred to ? let token = new Token(i,this);

this

Gonzalo Torres del Fierro
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

hello, I had the same question, and I solved it; but then I had a new question, posted ( you can check it as well), which Iยดm waiting for an answer, but I can help you with this one.

the code with: let token = new Token(i,this); mean: the teacher created a let ( the new variable "var" is no longer used "let" is the new standard for Js now specially in for loop); token, which is the type class Token, if you check the class Token, you will see how the constructor tells this: constructor(index, owner){ this.owner = owner; this.id = token-${index}-${owner.id}; this.dropped = false; so any Token class has an "index" and "owner", and in the current clas, where the code has being made ( wich is the class Player), our constructor is initialized as follow: constructor(name, id, color, active = false) { this.name = name; this.id = id; this.color = color; this.active = active; this.tokens = this.createTokens(21); }

and here you can see, that our teacher used

this.tokens = this.createTokens(21);

the right side of the equals sign is this.createTokens(21), so our teacher can call, this variable on the loop just calling using the keyword "this".

Richard Rowland
Richard Rowland
16,574 Points

"this" refers to the player object that is creating the token array.

The token constructor takes two parameters (index, owner). For each run through the loop, a new token object is created. i provides the tokens index. The owner is the player object that is creating the array. Therefore we are saying use "this" specific player object to provide the owner information.

2 Answers

Keegan Usher
Keegan Usher
26,226 Points

I think the confusing part is that in the video for setting up the Token constructor. Ashley never put in the parameters for the Token constructor. So if you followed the video the token constructor was left blank. The token constructor should have passed in two parameters "index" and "owner". The token constructor should look like this.

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

Now when creating the token method in Player, let token = new Token(i,this);, the "i" is the index being passed in to represent the id for that token and "this" is the owner parameter which is representing the player object.
Hope this helps!

Stephen Fowles-White
Stephen Fowles-White
10,457 Points

I was so sure that the Token constructor was missing the parameters and could not figure out where the (i, this) fit in. Thank you for this confirmation.

I was looking at the code and I was wondering if you could also just pass this.name or this.id from the player constructor to the createToken method and owner.id parameter rather than the whole Player object (this). Is there a reason for this? or is this explained later. Or have I missed a concept?

matt thurmond
matt thurmond
8,743 Points

Stephen Fowles-White I had the same question. It seemed like the code should be:

let token = new Token(i, this.owner);

and not

let token = new Token(i, this);

But when I re-checked, actually passing the entire player object using just "this" is what the teacher was going for. That way, the token's "owner" property ends up being the complete player object and not just the player's name. Also the token's id property is correctly accessed when creating a new token object since owner.id translates into Player.id, and not Player.name.id.

Stephen Fowles-White
Stephen Fowles-White
10,457 Points

Hi Matt,

Thank you for your response, I started to figure this out the better I got with this and objects, I just think it could have been explained a little better as you have.

Thank you for your confirming :)

Stephen