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

Alexandra Lukinicheva
Alexandra Lukinicheva
7,901 Points

Does it matter where we place getter methods within the class?

I've noticed that in Game.js, we placed get activePlayer() method before createPlayers() method. Does it matter if it's placed before or after? And if so, why?

class Game{ constructor(){ this.board = new Board(); this.players = this.createPlayers(); this.ready = false; }

get activePlayer(){
    return this.players.find(player => player.active);
}

createPlayers(){
    const players = [new Player("Player 1", 1, "#e15258",1,true),
                    new Player("Player 2", 2, "#e59a13",1)];
    return players;
}
startGame(){

}

}

1 Answer

I don't believe it matters as long as you are using function declarations and not function expressions in your class. (google function hoisting in javascript)

Generally though I see the getters and setters written at the top under the constructor method.