Estoy ingresando a ES6 y tratando de convertir una clase que tiene más de 2700 líneas en una colección de archivos pequeños y fáciles de leer .
Me gustaría mover un código de inicialización del constructor a sus propios métodos. Pero estoy un poco atascado. Considere este código:
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? } }Estoy confundido. ¿Qué opciones tengo para ejecutar código desde el constructor?
Podría, por supuesto, llamar a un método estático en su lugar, pero esto no me permitiría acceder a propiedades no estáticas.
Otra opción que veo es pedirle a la página que crea la instancia que luego ejecute initNonStaticStuff() (por ejemplo, mediante un método start()), pero prefiero omitir ese paso si es posible.
¿Qué me perdí?
Simplemente llámelo con this.initNonStaticStuff();