# EventTarget

The EventTarget interface is used by objects that can receive events and have listeners set up to handle those events. This interface provides three methods that are implemented by any event target. The most commonly used event targets are [element](https://developer.mozilla.org/en-US/docs/Web/API/Element) and their children, as well as [Document](https://developer.mozilla.org/en-US/docs/Web/API/Document) and [Window](https://developer.mozilla.org/en-US/docs/Web/API/Document). However, other objects such as XMLHttpRequest, AudioNode, and AudioContext can also be event targets.

In addition to implementing the EventTarget interface, many event targets like elements, documents, and windows also allow setting [event handlers](https://developer.mozilla.org/en-US/docs/Web/Events/Event_handlers) through "onevent" properties and attributes.

## Constructor

[EventTarget()](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/EventTarget)
Creates a new EventTarget object instance.

## Instance methods

[EventTarget.addEventListener()](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener)
Registers an event handler of a specific event type on the EventTarget.

[EventTarget.removeEventListener()](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener)
Removes an event listener from the EventTarget.

[EventTarget.dispatchEvent()](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent)
Dispatches an event to this EventTarget.

In an Azion function, `EventTarget` is the foundation behind the global `addEventListener` used to register the `fetch` handler, and it can also back your own objects that need to emit and listen for custom events during request processing.

## Use cases

- Building custom objects that dispatch and listen for events while a request is being handled on Azion's global network.
- Composing event-driven logic on top of the same interface that powers the runtime's `fetch` event.
- Adding and later removing listeners dynamically to control which handlers react to a given event.

## Related resources

- [EventTarget on MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget)
- [Runtime APIs](/en/documentation/products/applications/functions/runtime-apis/javascript/fetch/)

For more information on [EventTarget](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) visit MDN Web Docs.