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

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

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