我看到传递对象方法作为setTimeout参数的问题。 我知道里面的嵌套函数,这需要手动设置的范围,但如果我直接传递函数对象,在我的情况this.counting。什么是需要声明匿名函数作为第一个参数,this.counting已经是一个函数。不起作用?
Mozilla也使用函数(msg){self.remind(msg);}而不是this.remind在setTimeout的第一个参数中。
function Timer(count,start){
this.count = count;
this.start = start;
}
//below code works
Timer.prototype.counting = function(){
var self = this;
setTimeout(function(){self.counting();},this.start);
console.log(this.count);
this.count++;
};
//below code doesn't work
/*
Timer.prototype.counting = function(){
setTimeout(this.counting,this.start);
console.log(this.count);
this.count++;
};
*/
var t1 = new Timer(0,1000);
t1.counting();
var t2 = new Timer(100,1000);
t2.counting();