I want to call a class function from within a class, which I previously attached to a button-object that resides within the class (the following code works but the scope in callMyClassFunction is on button and I want it to be the Carousel):
//Examplecode 1
Carousel.prototype.addEventListeners = function(){
this.button.on('click', this.callMyClassFunction);
}
Carousel.prototype.callMyClassFunction = function(){
console.log(this);
}
If I bind the function like that it works (the scope of this is the class instance):
//Examplecode 2
Carousel.prototype.addEventListeners = function(){
this.button.on('click', function(){
this.callMyClassFunction()
}).bind(this);
}
But I'd rather want to add the click-eventlistener with a reference (like in Examplecode 1) since I want to call removeListener in yet another function:
//Examplecode 3
Carousel.prototype.removeEventListeners = function(condition){
if(condition){
this.button.removeListener('click', this.callMyClassFunction);
}
}
Any help is much appreciated!