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

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

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