liveBook · Manning

Static getter

As explained in Web Components in Action

A static getter is a JavaScript getter declared on a class or class-like object and accessed without creating an instance.

Event names are strings, but repeated strings become difficult to maintain as an event bus grows. A static getter gives a class-level constant-like name that callers can reuse without creating an instance. section 14.6

Centralizing an event type

Define the event name once: `static get PLAYLIST_UPDATE_EVENT() { return 'onPlaylistTimeUpdate'; }`. Dispatch with `new CustomEvent(WorkoutPlanData.PLAYLIST_UPDATE_EVENT, { ... })`, and listen with `EventBus.addEventListener(WorkoutPlanData.PLAYLIST_UPDATE_EVENT, callback)`. The getter is accessed through `WorkoutPlanData`, not through a created workout-plan object.

How Web Components in Action covers it

How deep it goes
Shows the declaration and its use in dispatching and listening, but does not explain getter internals.
Examples
Uses the PLAYLIST_UPDATE_EVENT getter for workout-plan dispatch and event-bus listener registration.
What you can do after
Apply it via JavaScript by defining and reusing a class-level event-name getter.
How it connects
Connects static getters with event buses, CustomEvent, and DOM events.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Components in Action. The section links point to the parts of the book it draws on.