I have a module function which sets up an addEventListener function and currently uses a global variable in the addEventListener function. I want to encapsulate this in an Object. But I can't see how to access the property from within the addEventListener function because 'this' within the addEventListener doesn't refer to my object ?
i.e
let x=0;
function mine (){
window.addEventListener('click',listener);
function listener(){
dosomething with x;
}
}
function setX(y) {x=y};
export {setX};
to
function Mine() {
this.x=0;
window.addEventListener('click',listener);
function listener(){
dosomething with this.x;
}
}
Mine.prototype.setX = function(y) {this.x=y);
export {Mine};
the latter does not work because in a listener 'this' does not refer to 'Mine' (I think).
Any help appreciated.