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 Rendering the Spaces, Board, and Tokens Solution

Accessing Player properties

Hi,

token.style.backgoundColor = this.owner.color;

~ 4:50 mark in the video Ashley says that the double-dot notation for this.owner.color makes sense there because the this.owner property is holding the Player object.

The Player object holds the color property so therefore we can use this.owner.color from Token.js to access the color property in Player.js

The double-dot part makes total sense but at what point in this project did we establish that ‘color’ is a property stored accessible to ‘owner’? How are we able to move across the files and pull properties from ‘Player’ and use them in ‘Token’?

Thanks for any input!

SL

1 Answer

Simon Coates
Simon Coates
8,377 Points

I haven't taken the video series, but I looked at the prior solution. At that point, the Game creates players, feeding in colors using hex values (eg '#e15258')

new Player('Player 1', 1, '#e15258', true)

The player class stores the color in the color field

class Player {
    constructor(name, id, color, active = false) {
        this.name = name;
        this.id = id;
        this.color = color;

The player also creates tokens, feeding in the the current object (of type player) as "this"

let token = new Token(i, this);

The second param for token is store as owner

class Token {
    constructor(index, owner){
        this.owner = owner;

So, implicitly, on a token, you can write this.owner to access a player, which will contain a color attribute. And you can assign the value contained therein to a property on the token.

Woah, Simon! Thank you for that. I continued on with the project and just let that be a black-box (one of many!) but your response REALLY helps.

Thank you!