# 章序号/节序号/节/笔记序号
- Objective:解决什么问题
# 概念阐释
# 举例子
# 类比、比较与对比
# 问题
- [练习题](https://www.codecademy.com/courses/introduction-to-javascript/projects/team-stats)
1. Populating Data 填充数据
2. Getting Data 获取数据
3. Adding Data 添加数据
我们想要向我们的团队添加一个新球员。在team对象中添加一个 .addPlayer() 方法。该方法应该传入三个参数:newFirstName、newLastName和newAge。
在该方法内部,通过将三个参数设置为对象的三个属性值:firstName、lastName、age,创建一个player对象。最后,将player对象添加到team的_players数组中。
# 问题答案
添加数据注意几个易错点:
- get 方法的使用
- 对象中的方法也是参数
```js
const team = {
_players: [{
// 答案中没有使用player1,直接是{firstName,lastName,age}
player1: {
firstName: "harry",
lastName: "zhu",
age: 35,
},
player2: {
firstName: "xiaorou",
lastName: "huang",
age: 30,
},
player3: {
firstName: "happy",
lastName: "chen",
age: 29,
}
}],
_games: [
{
game1: {
opponent: "york",
teamPoints: 34,
opponentPoints: 29
},
game2: {
opponent: "sentinel",
teamPoints: 45,
opponentPoints: 30
},
game3: {
opponent: "candobear",
teamPoints: 45,
opponentPoints: 60
}
}
],
get players() {
return this._players;
},
get games() {
return this._games;
},
//方法即是函数,所以这里写的都是参数
addPlayer(newFirstName, newLastName, newAge) {
const player = {
firstName: newFirstName,
lastName: newLastName,
age: newAge,
};
return this.players.push(player);//get关键字获取了属性信息,所以这里的新对象push到players,而不是_players属性中
},
addGame(newOpponent,newTeamPoints,newOpponentPoints) {
let game = {
opponent: newOpponent,
teamPoints: newTeamPoints,
opponentPoints: newOpponentPoints
}
return this.games.push(game);
}
};
team.addPlayer('Bugs','Bunny',76);
//使用get方法访问_players属性
console.log(team.players);
team.addGame('Titans',100,98);
console.log(team.games);
```
# 备注(经验集/错误集)
## summary
解决的怎么样?
## 参考资料