In order to ensure scope-busting for a specific listener in my application, I have implemented the following code:
// within the prototype of MyClass
var self = this;
myElement.addEventListener("stuff", function(e){self.doStuff(e)});
By doing this, I am able to achieve the desired binding for doStuff
.
However, an issue arises when attempting to use removeEventListener
. It seems that the signatures of the native functions are causing complications.
// within a different prototype of MyClass
var self = this;
myElement.removeEventListener("stuff", function(e){self.doStuff(e)}); // does not work
If I create a separate function that encompasses all of my scope-busting code, the this
binding in that code will be unwantedly tied to the object of
myElement</code. Hence, my question is: How can I enforce listener scope while still being able to remove an added event listener?</p>
<p>*Please note that the use of <code>global
or static
variables in any way is restricted due to the specific project requirements (otherwise, solving this issue would be straightforward!)