Übergabe this.method in setTimeout funktioniert nicht?

Ich sehe ein Problem mit der Übergabe der Objektmethode als Argument an setTimeout. Ich weiß, dass der Umfang der verschachtelten Funktion manuell festgelegt werden muss, aber was ist, wenn ich das Funktionsobjekt direkt übergebe, in meinem Fall this.counting. Um eine anonyme Funktion als erstes Argument zu deklarieren, ist this.counting bereits eine Funktion.

Mozilla Verwendet auch function (msg) {self.remind (msg);} anstelle von this.remind im ersten Argument von 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();

Antworten auf die Frage(1)

Ihre Antwort auf die Frage