In jQuery, when you set an event, you are able to namespace it. This means (if you want) you can have multiple resize window events, for example, and be able to unbind them individually without unbinding all events on that selector.
Example of jQuery namespacing:
$(window).on('scroll.myScrollNamespace, function() ...
I'm wondering how I can create a namespace in plain JavaScript. This obviously would not work:
window.addEventListener('resize.myScrollNamespace', function() ...
If instead of an anonymous function:
you use a named function:
then you may be able to use:
Make sure that you have
myScroll
in scope. When you remove the listeners in a different place than you add them, maybe you should define your functions in some outer scope and use their names inaddEventListener
in the same way as in theremoveEventListener
:If you want to be able to remove many listeners at once, then you will have to store them in some array and call removeEventListener for each of its elements.
See the
EventTarget.removeEventListener()
documentation.