大网数据广告活动
Web 标准与前端开发

CustomElementRegistry: CustomElementRegistry() constructor

· 52CE 我爱测

The **`CustomElementRegistry()`** constructor creates a new CustomElementRegistry object for scoped usage.

Web Components

The CustomElementRegistry() constructor creates a new CustomElementRegistry object for scoped usage.

The constructor is specifically used for creating scoped custom element registries that limit custom element definitions to a particular scope, such as an element or ShadowRoot.

[!NOTE] The global CustomElementRegistry object associated with a Window is not created using this constructor; it is automatically created when the window is set up, and is accessible via the window.customElements property.

Syntax

new CustomElementRegistry()

Parameters

None.

Return value

A new CustomElementRegistry object.

Description

When you construct a CustomElementRegistry using new CustomElementRegistry(), the resulting registry is considered scoped. This means:

  • Custom element definitions added to it using CustomElementRegistry.define are not globally available. They only apply to nodes that have been associated with this registry.
  • It does not support the extends option in define() (for creating customized built-in elements). Attempting to use extends with a scoped registry throws a NotSupportedError DOMException.

To associate a scoped registry with a DOM subtree, you can use the CustomElementRegistry.initialize() method, pass it to Element.attachShadow(), or use the Document.createElement() method's customElementRegistry option.

Examples

Creating a scoped custom element registry

This example creates a scoped registry, defines a custom element on it, and passes the registry to Element.attachShadow(). When HTML containing <my-element> is added to the shadow root, the element is upgraded using the scoped registry's definition.

const myRegistry = new CustomElementRegistry();

myRegistry.define(
  "my-element",
  class extends HTMLElement {
    connectedCallback() {
      this.textContent = "Hello from scoped registry!";
    }
  },
);

const host = document.createElement("div");
document.body.appendChild(host);
const shadow = host.attachShadow({
  mode: "open",
  customElementRegistry: myRegistry,
});
shadow.innerHTML = "<my-element></my-element>";

console.log(shadow.querySelector("my-element").textContent);
// "Hello from scoped registry!"

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。