Team Stats Project

I’m doing this project and my question is I’m up to step 6 and I have code that works but the hint shows a different code. In my addPlayers method I push it to the _players property but the hint in the project shows it without the underscore _ which begs the question why does it work and which way is the proper way to use this?

const team = {
  _players: [
    {
      firstName: "Luffy",
      lastName: "Monkey",
      age: 19
    },
    {
      firstName: "Zoro",
      lastName: "Roronoa",
      age: 21
    },
    {
      firstName: "Sanji",
      lastName: "Vinsmoke",
      age: 21
    }
  ],
  _games: [
    {
      opponent: "Crocodile",
      teamPoints: 3,
      opponentPoints: 2
    },
    {
      opponent: "Gecko Moria",
      teamPoints: 3,
      opponentPoints: 1
    },
    {
      opponent: "Doflamingo",
      teamPoints: 3,
      opponentPoints: 3
    }
  ],
  get players(){
    return this._players;
  },
  get games(){
    return this._games;
  },
  addPlayer(firstName, lastName, age){
    const player = {
      firstName,
      lastName,
      age
    };
    this._players.push(player);
  }
};

team.addPlayer("John", "Doe", 13);
team.addPlayer("Jane", "Doe", 37);
console.log(team.players);
1 Like

I get the same result as you with both ways working which isn’t helping my understanding of the logic.

Did you find out the reason? Or understand it 6 months later? Could it have something to do with the getter being named players?

Well, there is a fundamental problem here. Javascript doesn’t enforce the user of getter. There are plenty of languages (c#, php, java) which do enforce using the getter (preventing you to mutate the property directly)

using this.players will use the getter

if you have a getter, you should use it is a reasonable rule of thumb.

Thanks for the reply.