我在javascript中有一个对象,我将调用它的 weeklyTargetConstraint()。问题是它将调用方法 displayWeeklyTargetPrompt(),但这不会在方法调用中携带对象引用。
为什么控制台打印出Window对象?如何正确地做到这一点?
function main() {
c = new Constraints(5,6);
c.weeklyTargetConstraint();
}
function Constraints(weekNum, dayID) {
//setup
}
Constraints.prototype = {
weeklyTargetConstraint: function () {
var state = $("#hfWeeklyFillOverride", weekNumber).val();
this.constraintQty(this.fillTarget, state, this.displayWeeklyTargetPrompt);
///this will call displayWeeklyTargetPrompt()...
},
displayWeeklyTargetPrompt: function () {
console.log(this); // this is the Window Object
console.log(this.weekNumber); // its a input with id = weekNumber
loadWeeklyFillTargetMsg(this.weekNumber);
$('#weeklyTargetConfirm').dialog('open');
},
// other stuff here
}