我有一个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
,发现当我在setInterval
中调用this.start
时,this
指向window
对象。为什么会这样?
setInterval
ÊòØwindow.setInterval
ÔºåÂõÝÊ≠§this
指的是window
„ÄÇ - kidwon