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

232
Vistas
How to add onto a class after its definition?

I have a big class, called game, and it has many nested classes and functions.

class Game {
  constructor(opts) {

    this.Thing = class {
      constructor() {
        //do stuff
      }
      //a ton of functions
    }
    //a ton of functions

  }
}

I would like a way to organize it and make it easier to develop.

I was thinking of splitting the class up into multiple files, but I don't know how that would work.

Is there any way for me split this class up into multiple files? If not how else can I organize it, is there a better way?

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

0

One way to make this less verbose is to define some (or all) of the functions within other files. Just make sure you use the function keyword instead of an arrow function so that when you reference this, you're referencing the class it eventually gets put into. For instance, you can do something like this in one file:

export function method() {}

and then include it like this in your main file:

import { method } from './method';

class BigClass {
  constructor() {}

  method = method
}

Any reference to this within the method will refer to the instance it's called on.

As for nested classes, you should just be able to export the class from another file like so:

export class OtherClass {
   constructor() {}
}
import { OtherClass } from './otherclass'

class BigClass {
  constructor() {}

  OtherClass = OtherClass;
}
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