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

87
Visualizações
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 à 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