Remove keydown event listener

Viewed 6355

Trying to remove the keydown event listener. It says I need to use a named function. Isn't doStuff() a named function? Why doesn't it work? I wan't to remove the keydown event if someone types they E key. So after E the console should not long anymore anything. What am I doing wrong?

(function(){
  'use strict'

  class Blubb {
      constructor() {
          this.init();
      }
      
      init() {
          document.addEventListener('keydown', (function(event) {
              this.doStuff(event);
          }).bind(this));
      }
      
      doStuff(event) {
          if(event.keyCode === 69) {
              console.log('remove event listener', event.keyCode);
              document.removeEventListener('keydown', (function(event) {
                  this.doStuff(event);
              }).bind(this));
          } else {
              console.log('blubb', event.keyCode);
          }
      }
  };
  
  new Blubb();

})();
<h3>Press key on keyboard</h3>
<h3>Press "E" to remove Event Listener</h3>

0 Answers
Related