Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!