So, I’m doing right now the project “Team Stats” of “Introduction to JavaScript”, of Codeacademy, and everything’s working fine, but here’s my question, because I think I’m not getting this:
Why can I push something on an array where I’ve only put a getter and not a setter? I though setter where for that, for modifiyng things.
So, here’s my code
const team = {
_players: [
{
firstName: 'Pablo',
lastName: 'Sanchez',
age: 11
}
],
_games : [
{
opponent: 'Broncos',
teamPoints: 42,
opponentPoints: 27
}
],
get players() {
return this._players
},
get games (){
return this._games
},
addPlayer(firstName,lastName,age) {
let player = {
firstName,
lastName,
age }
this.players.push(player)
}
}
team.addPlayer('Steph', 'Curry', 28)
team.addPlayer('Lisa', 'Leslie', 44)
console.log(team.players)
The line that I’m talking about is this.players.push(player).
Thanks!