I have the following object. I works until I use setinterval .
I realise that this
becomes the object timeout
class test{
constructor(){
this.counter = 0
}
startCounter(){
this.increment()
setInterval(this.increment, 1000)
}
increment(){
this.counter++
console.log(this.counter)
}
}
var t = new test()
t.startCounter()
Outputs:
1
NaN
NaN
NaN
NaN
NaN
NaN
How to access the correct `this` inside a callback? suggest that I should use var self = this
but ES6 does not support private variables