Using subclasses in a superclass, at runtime, without circular import ES6

Viewed 49

I'm using ES6, and Browserify if it matters

I have a class named Entity

I have a subclass named Explosion. In other words, Explosion extends Entity.

I have a method on Entity named explode, in which I would like to instantiate an Explosion.

I don't wish to import Explosion in Entity.js because this would create a circular dependency.

I don't actually need Explosion to be available right away at compile time. I won't need it until much later on in runtime.

What's the correct and clean way to make Explosion available to reference at runtime in methods on Entity, without importing it?

What I have tried

I read this article which makes sense. I've done the same with ES5 in a more roundabout way.

I did try export default () => class Explosion, which does work, and does make Explosion available as a class,

However checking if Explosion instanceof Entity will return false.

This isn't desirable as I have assertions in place to avoid any mistakes, which will throw on this check.

0 Answers
Related