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

215
Visualizações
what is the use case for static inside class in javascript

I am a beginner in Javascript and I didn't understand the concept of static variables or methods inside Class. I don't understand why and when we need them and what is the use case for them? for example this class

class Car {
  constructor(name) {
    this.name = name;
  }
  static hello() {
    return "Hello!!";
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

OOP (including classes) was recently added to JavaScript, and in my opinion, it's more like syntactic sugar that allows us to structure our data more easily, or perhaps just for the satisfaction of using the new keyword. These OOP features were adapted from other languages, such as Java.

Static methods allow other pieces of your code to access variables and methods from a class without creating an instance of it (not using the new keyword).

This is especially helpful when you want to retrieve data from a class without creating an instance or initializing the constructor.

Take, for example, a real world example of Web Components:

class WhyStatic {
  /* other methods... */
  static get observedAttributes() {
    return ["name", "why", "test"];
  }
}

This allows the custom elements function to access the observed attributes of an HTML element without creating an extra instance.

In other words, we can create non-instance specifc data for a class that gets accessed outside, and only outside, a class instance.

That's why we need static in OOP.

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