I am doing a Codecademy project (Team Stats) and in step 6 it tells you to create a method ‘addPlayer’ so you can later push it what that method did to an array, but why does it work if i have a getter named ‘players’ and my array is named ‘_player’ and i pushed it to ‘players’ getter and it worked anyway, shoudln’t it only work if i push it to the ‘_players array’.
Example:
const team = {
_players: [
{
firstName: 'Pablo',
lastName: 'Sanchez',
age: 11
},
{
firstName: 'Camilo',
lastName: 'Gurria',
age: 17
},
{
firstName: 'Salvador',
lastName: 'Dali',
age: 102
}
],
_games: [
{
opponent: 'The Reds',
teamPoints: 1,
opponentPoints: 30
},
{
opponent: 'The Browns',
teamPoints: 5.3,
opponentPoints: 40
},
{
opponent: 'The Whites',
teamPoints: 10,
opponentPoints: 69
}
],
get players() {
return this._players;
},
get games() {
return this._players;
},
addPlayer(firstName, lastName, age) {
let player = {
firstName: firstName,
lastName: lastName,
age: age
};
this.players.push(player);
}
};
team.addPlayer('Steph', 'Curry', 28);
team.addPlayer('Lisa', 'Leslie', 44);
team.addPlayer('Bugs', 'Bunny', 76);
console.log(team.players);
Should it not only work if it is pushed to ‘_players’
const team = {
_players: [
{
firstName: 'Pablo',
lastName: 'Sanchez',
age: 11
},
{
firstName: 'Camilo',
lastName: 'Gurria',
age: 17
},
{
firstName: 'Salvador',
lastName: 'Dali',
age: 102
}
],
_games: [
{
opponent: 'The Reds',
teamPoints: 1,
opponentPoints: 30
},
{
opponent: 'The Browns',
teamPoints: 5.3,
opponentPoints: 40
},
{
opponent: 'The Whites',
teamPoints: 10,
opponentPoints: 69
}
],
get players() {
return this._players;
},
get games() {
return this._players;
},
addPlayer(firstName, lastName, age) {
let player = {
firstName: firstName,
lastName: lastName,
age: age
};
this._players.push(player);
}
};
team.addPlayer('Steph', 'Curry', 28);
team.addPlayer('Lisa', 'Leslie', 44);
team.addPlayer('Bugs', 'Bunny', 76);
console.log(team.players);
P.S. I watched the tutorial and that didn’t answer my question, please help