An accessor method lets a property do work when code reads or assigns it. In a custom element, a getter can return a stored value, while a setter can store a new value and update what the user sees. section 3.1.3
Update a table by assigning data
A sortable table can receive headings as strings, but its rows come from an array assigned in JavaScript: st.data = [{make: 'Ford', model: 'Mustang', year: 1975}]. The data setter stores the array, clears the table body, and creates a row for each object. Its getter returns the stored array. section 4.2.10
Use attributes and properties together for primitive values such as heading strings. When a headings attribute changes, attributeChangedCallback assigns the headings property. The setter stores the value, updates the attribute, and rebuilds the header row. It first checks whether the value actually changed, preventing an update loop. section 4.2.8
Keep objects and arrays in properties rather than attributes: attribute values are strings, and encoding rich data as JSON has costs and limitations. A setter can also make a focused update instead of replacing an entire component. For example, a radio group's value setter checks the allowed values, updates its form value, and selects the matching radio button. section 4.2.10 section 3.10
Listing 4.7 SortableTable headings setter
set headings(headings: string) {
if (headings === this.#headings) return;
this.#headings = headings;
this.setAttribute("headings", headings);
const tr = this.shadowRoot!.querySelector("table thead tr")!;
tr!.innerHTML = "";
const values = headings.split(",").map((heading) => heading.trim());
values.forEach((heading, i) => {
const property = this.#propertyArray[i];
tr.appendChild(this.#makeTh(heading, property));
});
}
The headings setter shows the change check, attribute synchronization, and header-row update in one place. section 4.2.8













