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 Game Class Constructor Method Solution

Zijun Liao
Zijun Liao
13,409 Points

createPlayers()

Can I create the object in the players list by using code as below? I am not really sure when we should use .push() and when use array[index] to assign the value.

createPlayers() {
        this.players.push(new Player("Player 1","1","#e15258",true));
        this.players.push(new Player("Player 2","2","#e59a13"));
     }

1 Answer

Hi!

Your code:

createPlayers() {
        this.players.push(new Player("Player 1","1","#e15258",true));
        this.players.push(new Player("Player 2","2","#e59a13"));
     }

In the video, this.players is assigned to the function createPlayers(). In your code, you call "push" on "this.players". This won't work because push is a behavior that exists on arrays, not functions.