这是我用来使用window.setinterval递增intVariable值的代码。与箭头函数相混淆javascript
var Arrow = (function() {
function Arrow() {
this.intVariable = 1;
this.itemId = -1;
this.interval = 25;
}
Arrow.prototype.activateTimer = function() {
if (this.itemId === -1) {
window.setInterval(this.showTimer(), this.interval);
}
};
Arrow.prototype.showTimer = function() {
this.intVariable += this.interval;
console.log(this.intVariable);
};
return Arrow;
}());
var arrow = new Arrow();
arrow.activateTimer();
当我使用下面一行显示计时器函数被调用一次
window.setInterval(this.showTimer(), this.interval);
但是,当我将其更改为:
window.setInterval(() => this.showTimer(), this.interval);
它完美。
需要一些帮助,为什么它使用箭头功能。
是的,这工作。但我很困惑,我已经如何使用这个引用,然后有什么区别做了绑定(这)。也如果使用箭头方法like()=> showtimer()那么它也在工作。所以我需要知道为什么这两种方法工作/ –
@yashshukla解释更多的细节,编辑答案。 –
我已经理解了70%,但仍然困惑于此,()=>和绑定(这)不完全确定的功能。你知道任何书籍或博客,我可以找到这方面的好消息吗?非常感谢您的帮助 –