Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

88
Vistas
Use a certain object instance in multiple modules without global variables

I have an object instance that's used by several different modules in my code. The instance itself gets edited during different events, then the modules use it to redraw the page.

For example:

// index.js
import populateProjects from 'populateProjects.js'
import populateItems from 'populateItems.js'

let projects = new projectList() // Projects in index.js's scope

projects.addProject('Project 1')
projects.getProject(0).addItem('Item 1')

// Note how I need to pass the 'projects' into the modules below
populateProjects(projects)
populateItems(projects)

//I would prefer both modules to have access to the same instance of 'projects' so that I don't need to pass it into them:
populateProjects() //'projects' is imported in the modules
populateItems()    //regardless of where they're called

Right now, I keep passing it into the module functions when they're called, but that means that I need to always have it in the scope of the script that's calling the module. I can get around this by using global variables but I'd rather not pollute the namespace for other modules that don't rely on it.

Is there a way that I can make this instance accessible in the specific modules that need it and still have it editable? Maybe using import?

about 4 years ago · Juan Pablo Isaza
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 Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda