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

136
Views
Access parent method inside a module class in Javascript

I have main app file "main.js":

import CharacterModel from './CharacterModel';

export default class MainManager {
  constructor(socketio) {
    this.io = socketio;
  }

  setup() {
  
    const character= new CharacterModel();

  }
  
  function1(vars) {}
  function2(vars) {}
  etc...
}

And I have file "CharacterModel.js":

export default class CharacterModel{
  constructor() {
    this.name = "test"
    this.points= 10;
  }

  start() {
  
    // how to get function1, function2 etc. here?

  }
}

How in that case I can access from CharacterModel.js to all or many functions from MainManager? Whats the best way and best for perofrmance if there will be many instances of CharacterModel inside MainManager?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Without proper context and what are the real objects in your story it's hard to recommend what to do, but the obvious way would be to just pass the instance of the MainManager to each CharacterModel on the constructor, and let the CharacterModel do calls to function1/function2.

You probably want to separate the the function123... to a domain specific classes, and pass them in the constructor to each instance that requires access to this domain.

For example: Lets say that you develop a game and you have a ScoreManager and a UserManager, and also a ConfigManager, you can create all the instances inside the MainManager and pass whatever is required to the other instances (Via the constructor).

This is called dependency injection.

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!