Debug Team Stats

I can’t find my error in the following exercise. I watched the video too, but still no luck. It has something to do with the ‘get games’ line.

const team = {
 _players: [
   {firstName: 'Pablo', lastName: 'Sanchez', age: 11},
   {firstName: 'Dan', lastName: 'Marshall', age: 12},
   {firstName: 'Jaime', lastName: 'Dixon', age: 13}
 ],
 _games: [
   {opponent: 'Phillies', teamPoints: 42, opponentPoints: 27},
   {opponent: 'Aggies', teamPoints: 24, opponentPoints: 30},
   {opponent: 'Sharks', teamPoints: 40, opponentPoints: 25}
 ],

 get players(){
   return this._players;
 },

 get _games(){
   return this._games;
 },

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

 addGame(opponent, teamPoints, opponentPoints){
   let game = {
     opponent: opponent,
     teamPoints: teamPoints,
     opponentPoints: 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('Flyers', 25, 36);
team.addGame('Froot Loops', 20, 51);
team.addGame('Snowmen', 52, 42);

console.log(team._games);

this.games is undefined, your team object does not have a games property

this should be your getter, but for some reason you prefixed your getter with an underscore?

Ah yes! Thank you so much! That pesky underscore. I need to do more research on when and when not to use them. They are confusing to me. :slight_smile: Taking out the underscore was effective.

Lets start with this really simple example:

const myObj = {
  a: 3,
}

console.log(myObj.a);

a simple object with a property, straight forward right? Now lets say we want to implement a getter, we could do:

DO NOT run this code
const myObj = {
  a: 3,
  get a() {
    return this.a;
  }
}

except it isn’t going to work, because the getter will endlessly invoke itself

we could give the getter a very different name:

const myObj = {
  a: 3,
  get b() {
    return this.a;
  }
}

console.log(myObj.b);

although this works, a equals b is just confusing

so the underscore is a simple naming convention:

const myObj = {
  _a: 3,
  get a() {
    return this._a;
  }
}

console.log(myObj.a); // use the getter

now we know that _a property implements a getter, and the property (_a) and getter (a) do not have the same name

Actually if you brain isn’t overloaded yet, read this line:
The get syntax binds an object property to a function that will be called when that property is looked up.

from the MDN docs. If you can grasp that line, things should be a lot more clear

Yes!! You explained that so clearly! Thank you!

Bottom line, once we have a getter, use it. Always.

A binding is like a rope that we can pull. It is secured to an object that can be pulled into the execution context of the other object, the one holding the rope.