How to solve this basic ES6-module circular dependency problem?
Mikael Sand
msand at abo.fi
Sun May 28 12:48:41 UTC 2017
How about applying Inversion of Control by implementing Dependency
Injection using Constructor injection? Just make your C constructor take
two parameters A and B like this:
```js
// --- Module A
import C from './C';
export default class A extends C {
// ...
}
// --- Module B
import C from './C';
export default class B extends C {
// ...
}
// --- Module C
export default class C {
constructor(A, B) {
// this may run later, after all three modules are evaluated, or
// possibly never.
console.log(A);
console.log(B);
}
}
// --- Entrypoint
import A from './A';
import B from './B';
import C from './C';
const c = new C(A, B);
console.log('Entrypoint', C, c);
document.getElementById('out').textContent = 'Entrypoint ' + C + ' ' + c;
```
https://www.webpackbin.com/bins/-KlDeP9Rb60MehsCMa8u
https://en.wikipedia.org/wiki/Dependency_injection#Constructor_injection
https://en.wikipedia.org/wiki/Inversion_of_control#Implementation_techniques
More information about the es-discuss
mailing list