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

223
Visualizações
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 Respostas
Responde à pergunta

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 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