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?
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.