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

Why the constructor method of the class Game is not including the properties board and players?

Hello, I still don't understand why in the constructor method there are not arguments. My code is as follows including both arguments. Can somebody explain me please?

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

Thanks

2 Answers

Steven Parker
Steven Parker
231,269 Points

You have listed two parameters, but neither of them is used in code in the body of the constructor. So they can both be omitted. None are needed because the code in the body of the method has everything it needs already to establish three values for the instance (board, players, and ready).

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

I think I still don't understand all the JS jargon yet. Because none of the parameters are used inside the constructor they don't need to be passed as arguments? How is that different from the Player class where none of the parameters are reference again before the constructor curly braces close?

class Player {
    constructor(name, id, color, active = false) {
        this.name = name;
        this.id = id;
        this.color = color;
        this.active = active;
        this.tokens = this.createTokens(21);
    }
Steven Parker
Steven Parker
231,269 Points

In the "Player" example shown here, all constructor parameters are referenced in the code body.

Steven Parker
Steven Parker
231,269 Points

This is an intermediate-level course. Have you already done the Beginning JavaScript track or an equvalent? The basics of function parameters would be covered in those courses.

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

Ooooh boy this is embarrassing, Steven. I very much have completed those courses and I'm on Unit 4 of the FSJS TechDegree. I just cannot seem to wrap my head around OOJS (or maybe just "JS"! T_T). I just don't see the difference between a constructor like so:

constructor(name, id, color, active = false) {
        this.name = name;
        this.id = id;
        this.color = color;
        this.active = active;
        this.tokens = this.createTokens(21);

and one like this:

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

one has no parameters and the other does, but I'm not sure why. this.name = name so it needs parameter? but this.board = new Board() so I guess since it's not this.board = board it doesn't need a parameter? Ooof. I'm starting to feel a bit stupid.

Steven Parker
Steven Parker
231,269 Points

Seems like you have the right idea.. If the constructor was referencing "board", then "board" would need to come in as a parameter (or be a global).