Given the following class:
class TimerFunk {
constructor(someObject){
this.a = 1
this.someObject = someObject
}
funk(){
console.log(this.a)
if(this.a > 2){
return
}
this.a++
this.someObject.execCallback(this.funk)
}
}
When the following commands are executed:
t = new TimerFunk({execCallback:function(callback){callback()}})
t.funk()
it is intended that the property TimerFunk.a
is logged to the console until this.a>2
. However instead the loop only executes once:
VM2097:9 1
VM2097:9 Uncaught TypeError: Cannot read property 'a' of undefined
at funk (<anonymous>:9:25)
at Object.execCallback (<anonymous>:5:13)
at TimerFunk.funk (<anonymous>:13:25)
at <anonymous>:1:3
From debugging I have figured out that this is because on the first loop this
represents the object t
however, on the 2nd loop this
represents someObject
.
Is there any way I can fix this and allow me to access class properties from within the executed callback?