I've been playing around with es6 classes, and tried setting up a simple mouse class.
addEventListener
works, but for some reason I'm unable to remove them with removeEventListener
. I'm guessing this has something to do with context binding, but I can't figure out how to fix this.
'use strict'
class Mouser {
constructor() {
this.counter = 0
this.clicked = function (event) {
this.counter++
console.log('clicks:', this.counter)
if (this.counter >= 10) this.remove()
}
window.addEventListener('click', this.clicked.bind(this))
}
remove() {
console.log('Removing click listener') // this line runs ..
window.removeEventListener('click', this.clicked.bind(this))
}
}
var mouse = new Mouser()