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

瀏覽器相容性