2016-06-07 54 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);