2016-06-07 44 views
-2
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方法做些什麼改變?回調無法正常工作。需要返回對象屬性值

+3

嘗試'studentMarks.computeMarks.bind(studentMarks)'回調 –

+0

見http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-回調這個主題的一些幫助。 –

+0

仍然沒有工作@vp_arth。任何其他想法? –

回答

2
setStudentScore(40,50,studentMarks.computeMarks); 

您在這裏只傳遞computeMarks值作爲回調。它是一種功能,而不是原始的對象方法。沒有關於此功能相關的對象的信息。

當它被調用時,this會指向全局對象。

嘗試直接綁定上下文:

setStudentScore(40,50,studentMarks.computeMarks.bind(studentMarks)); 
2

嘗試

setStudentScore(40,50,studentMarks.computeMarks.bind(studentMarks)); 

在JS,稱這事,是 「擁有」 的JavaScript代碼的對象。就你而言,看起來就像是調用它的函數。你可以使用bind來告訴它在你的上下文中引用了什麼。 Read this if you want

+0

它仍然打印undefined @Scimonster。 –

0

這應該爲你

var studentMarks = { 
    mathScore : 0, 
    englishScore : 0, 
    totalScore : null, 
    computeMarks : function (mathScore, englishScore) { 
     this.mathScore = mathScore; 
     this.englishScore = englishScore; 
     this.totalScore = this.mathScore + this.englishScore; 
    }.bind(studentMarks) 
} 

function setStudentScore(score1,score2,callback){ 
    callback(score1,score2); 
} 

setStudentScore(40,50,studentMarks.computeMarks); 

在控制檯

+0

它返回'null'。 –

+0

綁定爲時尚早,不是嗎?對象尚未分配給變量。 –

0

工作試試這個工作:

var studentMarks = function() { 
var self = this; 
this.mathScore = 0; 
this. englishScore = 0; 
this.totalScore = null; 
this.computeMarks = function (mathScore, englishScore) { 
    self.mathScore = mathScore; 
    self.englishScore = englishScore; 
    self.totalScore = this.mathScore + this.englishScore; 
    console.log(self.totalScore); 
} 
}; 

function setStudentScore(score1,score2,callback){ 
callback(score1,score2); 
} 

var obj = new studentMarks(); 

setStudentScore(40,50,obj.computeMarks); 
console.log(obj.totalScore); //prints 90 
+0

它的工作原理,但約束是我不能改變打印到控制檯。我能做的唯一改變是1)在this.computeMarks函數中&2)在我打電話回調的兩行中。 –

+0

然後vp_arth提到了正確的做法。 – Nika

+0

'不能將打印改爲控制檯 - 你在開玩笑嗎? 「print」實現在哪裏,如果不是? –

0

你可能不喜歡這樣

var studentMarks = { 
 
    mathScore : 0, 
 
    englishScore : 0, 
 
    totalScore : null, 
 
    computeMarks : function (mathScore, englishScore) { 
 
     this.mathScore = mathScore; 
 
     this.englishScore = englishScore; 
 
     this.totalScore = this.mathScore + this.englishScore; 
 
     return this.totalScore; 
 
    } 
 
}; 
 

 
function setStudentScore(score1,score2,callback){ 
 
    callback(score1,score2); 
 
} 
 

 
setStudentScore(40,50,function(a,b){studentMarks.computeMarks(a,b)}); 
 
console.log(studentMarks.totalScore);

相關問題