deno.com

interface EventListenerObject

The EventListenerObject interface represents an object that can handle events dispatched by an EventTarget object.

This interface provides an alternative to using a function as an event listener. When implementing an object with this interface, the handleEvent() method will be called when the event is triggered.

Examples #

#
// Creating an object that implements `EventListenerObject`
const myEventListener = {
  handleEvent(event) {
    console.log(`Event of type ${event.type} occurred`);

    // You can use 'this' to access other methods or properties
    this.additionalProcessing(event);
  },

  additionalProcessing(event) {
    // Additional event handling logic
    console.log('Additional processing for:', event);
  }
};

// Using with any EventTarget (server or client contexts)
const target = new EventTarget();
target.addEventListener('message', myEventListener);

// Later, to remove it:
target.removeEventListener('message', myEventListener);

Methods #

#handleEvent(evt: Event): void