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

125
Vistas
Why can't I call a static method from the constructor?

I'm getting into ES6 and trying to turn a class that's over 2700 lines long into a collection of small, easy-to-read files.

I'd like to move some initialization code from the constructor into their own methods. But I'm a bit stuck. Consider this code:

export default class Thingy {
  non_static_prop;

  initNonStaticStuff() {
  }

  constructor(args) {
    this.non_static_prop = 'some non-static prop set by the constructor'; // ok
    console.log(this.non_static_prop); // works as expected
    // now can we get a method to do the same thing?
    initNonStaticStuff(); // fails:
      /**
       * `Uncaught ReferenceError: initNonStaticStuff is not defined`
       */
    // So I guess we'll have to initialize everything here.
    // But then how am I supposed to make my code smaller and more modular?
  }
}

I'm confused. What options do I have to run code from the constructor?

I could, of course, call a static method instead, but then this wouldn't let me access non-static properties.

Another option I see is to ask the page that creates the instance to then run initNonStaticStuff() (e.g. say by way of a start() method), but I'd rather skip such a step if possible.

What did I miss?

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

0

Just call it with this.initNonStaticStuff();

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