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。任何其他想法? –