liveBook · Manning

Protection by convention

As explained in Web Components in Action

Protection by convention is a software encapsulation practice that relies on users respecting intended privacy boundaries rather than enforcing them technically.

Protection by convention marks private variables, methods, or component DOM without technically enforcing privacy. Underscores and an open Shadow DOM signal intended boundaries, but users can bypass them when necessary. section 8.2

An open Shadow DOM as an escape hatch

An open component exposes its root through shadowRoot, allowing direct inspection or modification: document.querySelector('sample-component').shadowRoot.querySelector('.inside-component').innerHTML += ' has been hijacked'. This weakens the boundary but supports flexibility and automated testing. section 8.2

How Web Components in Action covers it

How deep it goes
Contrasts open and closed Shadow DOM, demonstrates bypasses, and stops short of treating closed mode as secure.
Examples
Demonstrates underscore privacy, open-root hijacking, closed-root access limits, and locally versus publicly stored root references.
What it assumes
Assumes familiarity with classes, components, DOM access, and Shadow DOM terminology.
What you can do after
Evaluate component privacy claims and choose open or closed Shadow DOM with awareness of bypasses and testing access.
How it connects
Connects Shadow DOM with Web Components, private class properties, shadowRoot, closed mode, and automated testing.

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.