Capítulo 651 de 859

Chapter 651: EventDispatcher

Core Idea

This modules allows to dispatch event objects on custom JavaScript objects.

Main repository: eventdispatcher.js

Code Example:

Code Examples

class Car extends EventDispatcher {
	start() {
		this.dispatchEvent( { type: 'start', message: 'vroom vroom!' } );
	}
};
// Using events with the custom object
const car = new Car();
car.addEventListener( 'start', function ( event ) {
	alert( event.message );
} );
car.start();
  • What it demonstrates: Typical usage of EventDispatcher.

Reference Tables

Constructor

Signature
new EventDispatcher()

Methods

MethodDescription
.addEventListener( type : string, listener : function )Adds the given event listener to the given event type.
.dispatchEvent( event : Object )Dispatches an event object.
.hasEventListener( type : string, listener : function ) : booleanReturns true if the given event listener has been added to the given event type.
.removeEventListener( type : string, listener : function )Removes the given event listener from the given event type.

Key Takeaways

  1. Key methods: addEventListener, dispatchEvent, hasEventListener, removeEventListener.

Connects To