For a particular listener in my application, I'm using the following code for scope-busting purposes:
// this is all in a prototype of MyClass
var self = this;
myElement.addEventListener("stuff", function(e){self.doStuff(e)});
That will get doStuff
to have the desired this
binding.
The problem shows up when I try to removeEventListener
. I suppose it's because the native function signatures must be different?
// in a different prototype of MyClass
var self = this;
myElement.removeEventListener("stuff", function(e){self.doStuff(e)}); // doesn't work
If I make a separate function that contains all of my scope-busting code, then the this
binding in that code will be to the unwanted object of myElement
. So the question is: How can I force listener scope and still be able to remove an added event listener?
*note using global
/ static
variables in any way is prohibited due to the nature of the project (otherwise this would be simple!)