CustomElementRegistry: getName() 方法

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

getName() 方法是 CustomElementRegistry 接口的一部分,它返回一个先前已定义的自定义元素的名称。

语法

js
getName(constructor)

参数

constructor

自定义元素的构造函数。

返回值

先前定义的自定义元素的名称,如果没有使用该构造函数定义自定义元素,则返回 null

示例

js
class MyParagraph extends HTMLElement {
  constructor() {
    let templateContent = document.getElementById("custom-paragraph").content;
    super() // returns element this scope
      .attachShadow({ mode: "open" }) // sets AND returns this.shadowRoot
      .append(templateContent.cloneNode(true));
  }
}

customElements.define("my-paragraph", MyParagraph);

// Return a reference to the my-paragraph constructor
customElements.getName(MyParagraph) === "my-paragraph";

规范

规范
HTML
# dom-customelementregistry-getname

浏览器兼容性