liveBook · Manning

Accessor method

As explained in Web Component Development with Modern Libraries and Tooling

A JavaScript accessor that defines behavior when a property value is assigned.

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

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Works through getters and setters that rebuild table rows, synchronize headings, and update radio selection; does not explore language-level accessor internals.
Examples
Shows car-table and sortable-table row updates, heading changes, and radio-group selection through property setters.
What it assumes
Defines object properties and demonstrates accessors from simple assignments; implementation examples use custom elements and DOM methods.
What you can do after
Build it from scratch in a custom element to refresh UI from assigned properties and synchronize primitive attributes.
How it connects
Connects accessors to custom elements, private properties, attributes, attributeChangedCallback, shadow DOM, and form-associated elements.

Also explained in

Compare how 14 books explain accessor method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Component Development with Modern Libraries and Tooling. The section links point to the parts of the book it draws on.