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 is 'this' passed to the new Token object inside of the for loop?

I think I understand the createTokens method (though it is quite confusing) but I don't understand this line of code....

let token = new Token(i, this);

If I am correct, the 'i' is passed to the Token object so that the number can be used in the ID? But why is 'this' passed to it? What purpose does this serve?

3 Answers

Hi Daniel,

It's been a while since I took that course, but if I remember correctly, passing this into the function passes the correct player object into the function, so that the tokens can be assigned to the correct person/player.

One way to get a better understanding is to log out this right before that function: console.log(this)

Seeing what object is printed to the console will take away some of the abstraction (which could be helpful).

Hey Brandon, just after posting, I looked back at the Token.js file, and you're absolutely right.

For a minute, I forgot where the properties were being passed to. Thanks!

Alex Hort-Francis
Alex Hort-Francis
17,074 Points

Yeah, this is just passing in a reference to the current object, I believe..