For a certain reason, I have to declare my classes this way:
const core = {
character: class {
constructor(arg) {}
}
}
Works pretty, but I don’t know how to extends the class ‘character’. I’m trying the way below, but it doesn’t work:
const core = {
character: class {
constructor(arg) {}
},
enemy: class extends core.character {
constructor(arg) {}
}
}
3
Answers
You are trying to use
core
inside its own initialization, which is not allowed in JavaScript. To fix it, you can initialize the extended classes after the base class:For the record: it’s a bit more work, but you can do things without class syntax (using composition).
You could simply instantiate core from a class. This seems more readable and versatile than some alternatives.