Skip to content

EventTarget ​

EventTarget represents an object that can fire events and be listened to. It's the base class of every object that fires events: application, scenes, windows, views, controls, and all other native classes inherit from it. It's provided by the runtime as a global class and can be created directly with new.

The event model has just three rules:

  • Events are dispatched only to the object the listener is registered on; there's no capturing or bubbling.
  • The only option of addEventListener() is once.
  • Listeners can only be functions.

Inheritance: EventTarget

Constructor ​

EventTarget()

Creates a standalone event target for dispatching events between parts of the app's own code.

Instance methods ​

MethodDescription
addEventListener()Registers a listener
removeEventListener()Removes a listener
dispatchEvent()Dispatches an event synchronously

Examples ​

js
const bus = new EventTarget();
bus.addEventListener('login', (e) => {
  console.log('Received', e.type);
});
bus.dispatchEvent(new Event('login'));

Result: before dispatchEvent() returns, the console prints Received login.

Comparison with the browser ​

BrowserNacre
The capture, passive, and signal options of addEventListener()None. Only once
A listener can be an object with handleEvent()Not allowed. Functions only
Capturing and bubblingNone. Events are dispatched only to the target itself

See also ​