Getter methods and Array.push property


#1

In the code below, i didn’t write “this._players.push(player)”, but if I log the value to it, console.log(_players), I can still get six players info as i did with this.players.push(player) and then console.log(players)
why? I did not add the new players to _players but just players.

const team = {
 _players: [
   {firstName: 'Pablo',
    lastName:'Sanchez',
    age: 11},
    {firstName: 'Pete', 
     lastName:'Wheeler', 
     age: 13},
   {firstName: 'Kathleen', 
     lastName:'Wei', 
     age: 13},
 ],
 _games: [
   {opponent: 'Broncos', 
    teamPoints: 42, 
    opponentPoints: 27},
    {opponent: 'Monkey', 
    teamPoints: 40, 
    opponentPoints: 25},
    {opponent: 'Tony', 
    teamPoints: 37, 
    opponentPoints: 28}
 ],
  get players() {
  return this._players;
},
  get games() {
  return this._games;
},
addPlayer(firstName, lastName, age) {
let player = {
      firstName,
      lastName,
      age
};
this.players.push(player);
},
addGame(opponent, teamPoints, opponentPoints) {
  let game = {
    opponent,
    teamPoints,
    opponentPoints
  };
  this.games.push(game);
},
};
team.addPlayer("Steph", "Curry", 28);
team.addPlayer("Lisa", "Leslie", 44);
team.addPlayer("Bugs", "Bunny", 76);
console.log(team.players);
team.addGame("Joe", 38, 46);
team.addGame("Jack", 47, 38);
team.addGame("Thomas", 32, 29);
console.log(team.games);

#2

To follow along with what the environment will allow, let’s create a couple of setters…

set players (newPlayer) {

}
set games (newGame) {

}

Given that we have getters already in play its a slam dunk the we can make this work.

addPlayer(firstName, lastName, age) {
  let player = {
    firstName: firstName,
    lastName: lastName,
    age: age
  };
  this.players = player;
},

given,

    set players (newPlayer) {
       this._players.push(newPlayer);
    }