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

289
Visualizações
Create element from its constructor (without knowing the element tag)

Is it possible to create an element if you have its constructor but don't know its tag name?

Example:

class CustomElement extends HTMLElement {}
customElements.define(genRandomString(), CustomElement);

Is it possible to create a new instance of CustomElement? Or can you obtain its tag name in any way?

I'm asking this because I'm not sure what's the correct way to pass custom elements to functions that need them: do I need to pass around both the name and the constructor, in case one may need to either create an element (which requires the name) or to check whether an existing element is of this kind (which requires the constructor)?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Is this what you are asking for:

let foo = "bar";
window[ genRandomString() ] = foo;

And then you want to know what getRandomString produced?

Only when you monitor window can you capture what is added to it.

Same with WebComponents, only when you hook into customElements.define can you record what names are used.
In other words: mess with its prototype and potentially breaking its default execution.

But this only works from the moment you are in control; you could have missed all Web Components created already, at that time.

Can you explain WHY you want to do this?

about 4 years ago · Juan Pablo Isaza Relatório

0

Is it possible to create an element if you have its constructor but don't know its tag name?

If you have access to the constructor (like you said you have), you can just use

const instance = new CustomElement();

Please note that this might fail if the constructor doesn't conform to Custom Element constructor constraints.

about 4 years ago · Juan Pablo Isaza 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