I have two classes, exported in single instance pattern
// classA.js
let ClassB = require('./classB')
let ClassA = class ClassA {
constructor () {
this.name = 'ClassA'
}
getName () {
return this.name
}
getOtherName () {
return ClassB.getName()
}
}
module.exports = (function() {
return new ClassA()
})()
// classB.js
let ClassA = require('./classA')
let ClassB = class ClassB {
constructor () {
this.name = 'ClassB'
}
getName () {
return this.name
}
getOtherName () {
return ClassA.getName()
}
}
module.exports = (function() {
return new ClassB()
})()
In both instances, running getOtherName()
will throw an error, as the other class object is an empty object.
I've read this post, which has some information for solving this with pre-ES6 class definitions (prototype style), however none of those solutions seem applicable.
For example, you can't export the class before defining the methods. I can think of ways to solve it where the classes aren't singletons, but requiring a single instance of each class makes it harder I think.
What's the best solution for this?