function StudentReport() {
var grade1 = 4;
var grade2 = 2;
var grade3 = 1;
var getGPA = function() {
return (var grade1 + var grade2 + var grade3) / 3;
};
}
var myStudentReport = new StudentReport();
for(var x in myStudentReport) {
if(typeof myStudentReport[x] !== "function") {
console.log("Muahaha! " + myStudentReport[x]);
}
}
console.log("Your overall GPA is " + myStudentReport.getGPA());
答案 0 :(得分:2)
使用变量时,只有在第一次定义变量时才使用var
。
return (var grade1 + var grade2 + var grade3) / 3;
^^^ ^^^ ^^^
但是您的代码在检查功能时遇到了更多问题,但您没有将其编码为任何问题。
for(var x in myStudentReport) { <-- why are you looking through the function?
你想要做这样的事情
function StudentReport() {
var grade1 = 4;
var grade2 = 2;
var grade3 = 1;
this.getGPA = function() {
return (grade1 + grade2 + grade3) / 3;
};
}
var myStudentReport = new StudentReport();
console.log(myStudentReport.getGPA());
答案 1 :(得分:1)
这里没有任何意义,摆脱那里的var
。
return (var grade1 + var grade2 + var grade3) / 3;