liveBook · Manning

Initializer

As explained in Web Components in Action

A constructor is a function or method invoked to initialize a newly created object of a particular class or type.

An initializer, commonly called a constructor, runs when a class is instantiated. In a Web Component, it runs when the element is created, before the element is added to the DOM. section 2.1 section 4.2

When defining a custom element, keep these timing rules in view:

  • Call super() first when extending HTMLElement; this invokes the inherited constructor. section 4.2
  • Create an element with document.createElement() to trigger its constructor without adding it to the page. section 4.2
  • Use connectedCallback when work depends on the page DOM; ordinary innerHTML and related element DOM APIs are not yet available during construction. section 4.0 section 8.2
  • With Shadow DOM, attach a shadow root in the constructor and initialize its contents immediately. section 8.2

A custom element initializer

class MyCustomTag extends HTMLElement { constructor() { super(); this.serviceURL = 'http://company.com/service.json'; this.counter = 0; this.error; } connectedCallback() { this.innerHTML = '<h2>' + this.getAttribute('title') + '</h2>'; } } The constructor establishes component state. The connectedCallback performs page-DOM work after insertion. Writing innerHTML directly in the constructor fails for a custom element, but writing to shadowRoot after attachShadow() is supported. section 4.2 section 8.2

Listing 9.14. Slider component constructor
constructor() {                                     #1
    super();
    this.attachShadow({mode: 'open'});              #2
    this.shadowRoot.innerHTML =
       Template.render();                           #3
    this.dom = Template.mapDOM(this.shadowRoot);

    document.addEventListener('mousemove', e => this.eventHandler(e));
    document.addEventListener('mouseup', e => this.eventHandler(e));
    this.addEventListener('mousedown', e => this.eventHandler(e));
}

Trace a complete constructor-based component: call super(), attach an open shadow root, render a template, map its DOM, and register event handlers. section 9.5

How Web Components in Action covers it

How deep it goes
Explains instantiation timing, inheritance, DOM restrictions, state setup, and Shadow DOM initialization, but not constructor internals.
Examples
Uses document.createElement(), custom-element alerts, property initialization, failed innerHTML assignment, and a Shadow DOM slider.
What it assumes
Assumes familiarity with JavaScript classes and inheritance while demonstrating HTMLElement constructors and custom-element creation through browser examples.
What you can do after
Build it from scratch by defining a custom-element constructor, calling super(), initializing state, and selecting the correct DOM lifecycle point.
How it connects
Connects constructors with classes, inheritance, HTMLElement, connectedCallback, the DOM, and Shadow DOM.

Also explained in

Compare how 9 books explain initializer →

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.