If you can somehow separate the two classes, for example, allow another implementation of the Baz class for the bar, then I think the best way and you should go for it.
But if you really want to be able to reference exports.Baz as Baz , then there is one way that I could think of using with .
It is said that using with usually bad practice and should be avoided, I would not use it in my own code, but this is one way to solve it and it could even be using legimate if you know what you are doing.
Here he is:
with(exports) { exports.Bar = function Bar() { console.log('this is bar!'); this.baz = new Baz(); }; exports.Baz = function Baz() { console.log('original baz!'); }; }
In another module, if you change foo.Baz to something else, the Baz inside foo will also look at it.
I would still recommend finding a way to make these two classes independent of each other, and then you can just provide Bar with any Baz implementation you want.
source share