Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Hide Internal Members

Jan Miksovsky edited this page Sep 23, 2019 · 2 revisions

Checklist » API

✓ Hide Internal Members

Does the component avoid exposing internal members in its public API?

A component should only expose methods and properties in its public API that are intended for use by page authors. Exposing methods and properties that are only intended for internal use can be confusing to page authors, and also encourage them to rely on internals that you may want to change over time.

Generally speaking, methods and properties exposed with string names are trivially easy for page authors to discover, e.g., in a browser's debug console. Even if you try to signal that a member is private by prefixing its name with an underscore (_foo), page authors will still find such members, and despite your intentions come to depend on them.

There are a variety of alternatives in JavaScript for hiding members from a component's public API:

  • Identify internal members with Symbol keys instead of string names.
  • Use JavaScript module scoping to keep internal members hidden outside the module that defines the component.
  • Use JavaScript function closures to hide internal members.
  • Store private data on element instances using a WeakMap.
  • Identify JavaScript private fields and private methods using the # syntax.
Clone this wiki locally