Calling methods several time

Hi at all,

example: We have a method that fills an object with data. If you call this method via and give it the appropriate parameters, the object is only filled once. Shouldn’t the data be appended to the existing data each time the method is called?

Like it was in the team-stats-project:

const team ={
  firstName: 'Pablo',
  age: 11
  opponent: 'Broncos',
  teamPoints: 42,
  opponentPoints: 27

addGame(opponentsName, yourPoints,opponentsPoints) {
  let game = {
team.addGame('Test1', 20, 30)
team.addGame('Test2', 40, 20)
team.addGame('Test3', 70, 10)

Output has been everytime I called console.log…:

{ opponentsName: ‘Test1’, yourPoints: 20, opponentsPoints: 30 },
{ opponentsName: ‘Test2’, yourPoints: 40, opponentsPoints: 20 },
{ opponentsName: ‘Test3’, yourPoints: 70, opponentsPoints: 10 } ]

You have some errors in your variable names when you try to access them once again. Check the following and you will be able to figure out what errors you have committed. I have left out the _players object for convenience. Moreover, it seems you haven’t used the this keyword in a proper manner.


For proper usage of this keyword: this keyword usage in JavaScript

The issue has nothing to do with, this. And, there is not enough code present to tell us whether a setter exists or not so we cannot draw any conclusions.

Please post the entire code or a GIST of it so the real problems can be sussed out.

If the code does not have a setter then addGame() is the setter, so it will be necessary to access the backing variable (the one with the _ prefix.