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

85
Views
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
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!