• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

94
Vistas
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 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda