Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
Unattached web component cannot call class methods if created before definition

Looks like if a custom element is created before it's been defined, it will have the HTMLElement constructor, and retain this even after the definition is completed.

Creating that element again will have the correct constructor, see code example below.

const comp = document.createElement("my-comp");

console.log('First element before definition: ', comp.constructor.name);

customElements.define("my-comp", class MyComp extends HTMLElement {
    constructor() { super(); }
    myMethod() { return; }
  }
);

console.log('First element after definition: ', comp.constructor.name);

try { comp.myMethod() }
catch (e) { console.error(e) }

const secondComp = document.createElement("my-comp");
console.log('Second element: ', secondComp.constructor.name);

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

If you are sure you have now defined the component, you can use !customElements.get(comp) && customElements.upgrade(comp) to upgrade the component so that its constructor is changed to the custom element's class.

const comp = document.createElement("my-comp");

console.log('Component before definition: ', comp.constructor.name);

customElements.define("my-comp", class MyComp extends HTMLElement {
    constructor() { super(); }
    myMethod() { return; }
  }
);

console.log('Component after definition: ', comp.constructor.name);

!customElements.get(comp) && customElements.upgrade(comp)

console.log('Component after upgrade: ', comp.constructor.name);

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda