JavaScript custom events.
// adding events to a custom object
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();
Creates EventDispatcher object.
Adds a listener to an event type.
Checks if listener is added to an event type.
Removes a listener from an event type.
event — the event that gets fired.
Fire an event type.
There is a set of puzzles that implements similar functionality in a visual manner:
For more info on how to obtain the source code of this module see this page.