liveBook · Manning

Constructor method

As explained in Vanilla Web

A JavaScript function intended to create and initialize object instances when invoked as a constructor.

A custom element’s constructor runs when the browser creates or upgrades the element. It is a place to initialize internal state, attach a shadow root, and bind handlers. Connection is a separate step: connectedCallback() runs when the element is inserted into a document. section 9.2.1

Register the class or constructor function with customElements.define(name, constructor). The name must contain a hyphen and must not already be registered. Once registered, the browser can instantiate the class from its HTML tag or through document.createElement(). An unregistered tag does not raise an error; it remains an empty HTMLElement rather than an instance of the intended class. section 7.6.3

Listing 9.2 A custom element showing the main lifecycle callbacks
class LifecycleDemo extends HTMLElement {
  static observedAttributes = ["status"];

  constructor() {
    super();
    console.log("1. constructor");
    this.attachShadow({ mode: "open" });
  }

  connectedCallback() {
    console.log("2. connectedCallback");
    this.render();
  }

  attributeChangedCallback(name, oldValue, newValue) {
    console.log(`3. attributeChangedCallback: ${name} changed`);
    if (this.isConnected) {
      this.render();
    }
  }

  disconnectedCallback() {
    console.log("4. disconnectedCallback");
  }

  adoptedCallback() {
    console.log("5. adoptedCallback");
  }

  render() {
    this.shadowRoot.innerHTML =
      `<p>Status: ${this.getAttribute("status") || "none"}</p>`;
  }
}

customElements.define("lifecycle-demo", LifecycleDemo);

The example calls super() before attaching a shadow root in constructor(). It renders after connection and checks isConnected before rendering in response to an attribute change, which can occur before connectedCallback(). section 9.2.1

Prefer attributes and properties over arguments passed with new: constructor arguments will be undefined when the same element is instantiated from HTML. Keep the component’s public contract in its attributes, properties, methods, and events rather than relying on a JavaScript-only creation path. section 7.6.3 section 7.5.1

How Vanilla Web covers it

How deep it goes
Shows registration, creation paths, constructor setup, and callback timing; does not detail constructor internals beyond custom elements.
Examples
Uses a registered Bitcoin-symbol element and a lifecycle-logging element with a shadow root and guarded rendering.
What it assumes
Assumes familiarity with JavaScript classes and the DOM while showing how custom-element registration and lifecycle callbacks fit together.
What you can do after
Build it from scratch: register a custom element, initialize it in constructor(), and defer connection-dependent work.
How it connects
Links constructors to custom elements, Shadow DOM, connectedCallback(), attributeChangedCallback(), and component attributes and properties.

Also explained in

Compare how 18 books explain constructor method →

Related concepts

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