我有这个 Javascript 构造函数-
function TestEngine() {
this.id='Foo';
}
TestEngine.prototype.fooBar = function() {
this.id='bar';
return true;
}
TestEngine.prototype.start = function() {
this.fooBar();
}
TestEngine.prototype.startMethod = function() {
inter = setInterval(this.start, 200);
}
var test = new TestEngine();
test.startMethod();
给我这个错误 -
Uncaught TypeError: Object [object global] has no method 'fooBar'
我尝试console.log
发现,当我从 inside 调用时this.start
,setInterval
指向this
对象window
。为什么会这样?