此代码位于 KnockoutJS 视图模型中:
function myViewModel()
{
this.prop1 = ko.observable(123);
this.prop2 = ko.observable("Hello");
..
..
}
myViewModel.prototype.func1 = function()
{
alert(this.prop1()); //works fine here
myTimer = setTimeout(function()
{
alert(this.prop1()); //this.prop1() raises an undefined error
}, 3000); //from console: Uncaught TypeError: undefined is not a function
};
为什么在计时器回调中无法识别该属性?看起来像一个范围问题,但我似乎无法解决它。