Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

293
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda