[英]How can I get the addStudent method to work correctly?
I have been up all night trying to find out what I am doing wrong.我整晚都在努力找出我做错了什么。 I have an Object 'studentScores' and a method addStudent, I get errors and if I don't get errors it just consoles the entire function.
我有一个 Object 'studentScores' 和一个方法 addStudent,我收到错误,如果我没有收到错误,它只会控制整个 function。 Here is my code-
这是我的代码-
let studentScores = {
studentOne: {
fName: 'Joey',
lName: 'Hendricks',
score: 100
},
studentTwo: {
fName: 'Tim',
lName: 'Izer',
score: 98
},
studentThree: {
fName: 'Amy',
lName: 'Krueger',
score: 69
},
addStudent: function(stuNum, name1, name2, stuScore){
// this.stuNum.push({
// stuNum:,
// fName: name1,
// lName: name2,
// score: stuScore
// });
return this.stuNum = {fName: name1, lName: name2, score: stuScore};
}
};
When I do it like this-当我这样做时-
console.log(studentScores.studentFour = {fName: 'Jimi', lName: 'Hendrix', score: 98});
It works perfectly.它完美地工作。 Can someone lead me in right direction to what I'm doing wrong?
有人可以引导我朝着正确的方向前进吗? I thank you so very much.
我非常感谢你。 Thank you.
谢谢你。
If key of object is variable you should use []
instead of dot.如果 object 的键是可变的,则应使用
[]
而不是点。
let studentScores = { studentOne: { fName: 'Joey', lName: 'Hendricks', score: 100 }, studentTwo: { fName: 'Tim', lName: 'Izer', score: 98 }, studentThree: { fName: 'Amy', lName: 'Krueger', score: 69 }, addStudent(stuNum, name1, name2, stuScore) { return (this[stuNum] = { fName: name1, lName: name2, score: stuScore }) } } studentScores.addStudent('studentFour', 'Jimi', 'Hendrix', 98) console.log(studentScores)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.