I'm trying to store Listener functions so that I can remove them later. I'm using a wrapper to add listeners. The issue is that the listeners are not removed. Why is that?
Below is some code:
classMyClass{
// add the wrapper to event listener
init(){
window.app.addEventListener = function(type, fn, opt){
obj.listeners.push({type: type, fn: fn, opt: opt});
console.log('addEvent ', l);
var l = obj.listeners[obj.listeners.length-1];
console.log(l);
window.addEventListener(l.type, l.fn, l.opt);
//window.removeEventListener(l.type, l.fn, l.opt);
};
}
removeListeners(){
let obj = this;
this.listeners.forEach(function() {
var l = obj.listeners[obj.listeners.length-1];
console.log('remove ', l);
// remove the listener
window.removeEventListener(l.type, l.fn, l.opt);
obj.listeners.pop();
});
}