我一直在研究codecademy中的Javascript,並對其中一個問題有疑問。在Javascript中使用對象
問:
寫兩個功能:
one creates an object from arguments
the other modifies that object
我的回答:
//First, the object creator
function makeGamePlayer(name,totalScore,gamesPlayed) {
//should return an object with three keys:
// name
// totalScore
// gamesPlayed
var myObject = {
"name": name,
"totalscore" : totalscore,
"gamesPlayed" : gamesPlayed
};
return myObject;
}
//Now the object modifier
function addGameToPlayer(player,score) {
//should increment gamesPlayed by one
//and add score to totalScore
//of the gamePlayer object passed in as player
var score = player[totalscore];
score =score+1;
player[totalscore] = score;
}
不知道在我的錯誤。需要改善這種解決方案..非常感謝一些指導...
在你的對象
有什麼問題時?如果你不知道錯誤是什麼,我們應該猜測它? – Th0rndike 2012-08-10 15:01:54
你說你必須給'player.totalscore'添加'score',但是你將'score + 1'分配給'player.totalscore'。另外,要小心括號記號,因爲它需要字符串:'player [「totalscore」]',而不是'player [totalscore]'。 – 2012-08-10 15:02:41
您應該在練習時閱讀[MDN JavaScript指南](https://developer.mozilla.org/en-US/docs/JavaScri)。尤其要看[使用對象](https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects)。 – 2012-08-10 15:10:27