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 Rendering the Game Getter Methods Solution

Trevor Maltbie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Trevor Maltbie
Full Stack JavaScript Techdegree Graduate 17,021 Points

How does this.tokens access the class Tokens for the dropped property?

get unusedTokens() {
      return this.tokens.filter(token => !token.dropped);
    }

I see that in the class Player we have this.tokens = this.createTokens(21) but how does the filter method get access to this.dropped in the class Tokens?

1 Answer

Steven Parker
Steven Parker
231,248 Points

The term "this" is only used inside the current class instance. Outside, the instance name is combined with the attribute name using the membership operator (the period).

So as the filter is checking the "this.tokens" array one at a time, each one is assigned the name "token" and "token.dropped" accesses its "dropped" property.

Daryl Monte
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Daryl Monte
Python Development Techdegree Graduate 25,409 Points

Hi Steven, I am still a bit confuse with the code above.

Where did we get the token in (token => !token.dropped) ? Is this related to the Token class in Token.js or is this a new variable?

Same in the activePlayer getter: return this.players.find(player => player.active) Where did the player in the parenthesis come from? Is this related to the Player class in Player.js or is this a new variable?

Thank you.

Steven Parker
Steven Parker
231,248 Points

In this case, token is the parameter of the arrow function being defined here (so yes, sort of a new variable).
And yes, the filter applies this function to each item in the tokens array.