Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,750 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
14,888 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,499 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