I've a bigger object where I save all important data of my main class. My project is getting kinda bigger so I've some seperated files/classes. All of them need to have access to my store object, which values gonna change from time to time.
I know I could just export/import my store to each class, but then the changes are kinda super global, what means if I instantiate my main class more than once and I change the store in the first instantiated main class, then the second instantiated main class would see the changes of my store too. Hope I could explain well enough.
Here is an very simple example with just 2 classes, but I've far more:
index.js
let ClassA = require('./ClassA')
let ClassB = require('./ClassB')
let store = {
propA: 1,
propB: 2
}
class mainClass {
constructor() {
console.log('main Class')
this.doSomething()
}
doSomething() {
let classA = new ClassA() // get/edit store there
let classB = new ClassB() // get/edit store there
classA.doSomething()
classB.doSomething()
}
}
let instantiatePorject = new mainClass()
ClassA.js
module.exports = class ClassA {
doSomething() {
// get/edit store here
}
}
ClassB.js
module.exports = class ClassB {
doSomething() {
// get/edit store here
}
}