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

222
Vistas
How do I access a property on a base class that is for a web component?

I would like to set the html namespace for the component on the Base class, but it seems you can't do NavBar.ns without creating an instance, which makes the web component not render.

import Base from './Base.js';

class NavBar extends Base {
  constructor() {
    super();
  }

  connectedCallback() {
    console.log('ns', this.ns);
    this.innerHTML = '...';
  }
}

console.log('ns2', NavBar.ns);  // undefined here
customElements.define(`${NavBar.ns}-navbar`, NavBar);

Base.js:

class Base extends HTMLElement {
  constructor() {
    super();
    this.ns = 'wce';
  }


}

export default Base;

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can't access Navbar.ns because you aren't creating an instance (object) of Navbar, and this.ns will be applied for each new call to navbar, e.g. const navbar = new Navbar(); navbar.ns; // <-- wce. You aren't creating an instance of Navbar, so you can create a static field, which allows you to access a field without creating a class instance.

In base.js:

class Base extends HTMLElement {
  constructor() {
    super();
  }
  static get ns() {
    return 'wce';
  }
}

export default Base;

static allows us to access class fields without, and only without creating a new instance.
Now, when you call NavBar.ns it will return "wce" using a getter.

However, you also wanted to be able to access .ns when you (customElements.define) create a new instance. You'll have to set an addition this call:

class Base extends HTMLElement {
  constructor() {
    super();
    this.ns = 'hello';
  }
  static get ns() {
    return 'world';
  }
}

export default Base;

In your case you can change both "hello" and "world" to "wce" if you want them to "work the same way."

Now, let's say you want to access ns, if you aren't creating an instance you would do:

NavBar.ns; // <-- "world"

But if you were creating an instance (like in customElements.define), you use the new operator to access this.ns:

const navbar = new NavBar();
navbar.ns; // "hello"

(I used "hello" and "world" instead of "wce" just for the illustration.)
Now with the added code you could use it as in your example:

                       /*  "wce-navbar"  */
customElements.define(`${NavBar.ns}-navbar`, NavBar);

Some docs about static.

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