var studentMarks = {
mathScore : 0,
englishScore : 0,
totalScore : null,
computeMarks : function (mathScore, englishScore) {
this.mathScore = mathScore;
this.englishScore = englishScore;
this.totalScore = this.mathScore + this.englishScore;
console.log(this.totalScore);
}
}
function setStudentScore(score1,score2,callback){
callback(score1,score2);
}
setStudentScore(40,50,studentMarks.computeMarks);
print(studentMarks.totalScore); //prints 'undefined'
打印语句应打印90,而不打印undefined
。我应该对computeMarks方法做些什么改变?回调无法正常工作。需要返回对象属性值
尝试'studentMarks.computeMarks.bind(studentMarks)'回调 –
见http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-回调这个主题的一些帮助。 –
仍然没有工作@vp_arth。任何其他想法? –