Team Stats Projects

Hello there,

I would like to know what why a get a Maximum call stack error when ever I try to run these codes.

let team = {

  _players : [

    {

      firstName: 'Bukayo',

      lastName: 'Saka',

      age: 19

      },

      { firstName: 'David',

      lastName: 'Luiz',

      age: 33

      },

      {

        firstName: 'Granit',

      lastName: 'Xhaka',

      age: 27

      }

  ],

  _games : [

    {

      opponent: 'Chelsea',

      teamPoints: 58,

      opponentPoints:55

    },

    {

      opponent: 'Liverpool',

      teamPoints: 58,

      opponentPoints:65

      },

      {

        opponent: 'Wolves',

      teamPoints: 58,

      opponentPoints:42}

  ],

  //getter method for _players

get _players() {

  return this._players

},

//getter method for _games

get _games() {

  return this._games

},

//method addPlayer

addPlayer (firstName, lastName, age) {

let newplayer = {

  firstName: firstName,

  lastName: lastName,

  age: age

}

team._players.push(newPlayer)

}

}

team.addPlayer('Dani','Ceballos', 24)

team.addPlayer('Kieran', 'Tierney', 24)

team.addPlayer('Bernd', 'Leno', 25)

console.log(team._players);

Also note that I followed the codecademy walkthrough in creating this solution hence I see no reason why It will not work.

according to getter documention:

The get syntax binds an object property to a function that will be called when that property is looked up.

then looking at this code:

get _players() {

  return this._players

},

//getter method for _games

get _games() {

  return this._games

},

here:

 return this._games

will attempt to gain/read the _games property, which has function bound to it (because of the getter), so this will call the function, leading to function endless calling itself (recursion)

1 Like