Ad
  • Custom User Avatar

    Handlers (handling functions) are the subscribers of your Event object. The invocation context of a function is a value you get when you reference this inside the function. The requirement is, this inside the handlers should point to this of the emit method, i.e. the instance of the Event object. The error tells you it's not happening in your case.

    This functionality is needed so you could subscribe and unsubscribe to the event from within your handlers, like this:

    const myEvent = new Event();
    
    myEvent.subscribe(() => {
      // notice the use of "this" instead of the explicit "myEvent"
      this.subscribe(/* ... */);
      this.unsubscribe(/* ... */);
    })