You might want anonymous classes if you know exactly what you're doing, you're creating a hierarchy of classes (i.e. things you want copies of) in a well-thought-out metaprogramming system, and there's no other elegant solution to extension, e.g.
{
myImplementation: class extends MyBaseClass {
someMethod(x) {
super().someMethod(x);
insert extended behavior
}
}
}
Of course you could implement the above with some abusive magic functions which use Object.assign
:
{
myImplementation: extendMagic(mySuper => ({
someMethod(x) {
mySuper.someMethod(x);
insert extended behavior
}
}))
}
or find some better way to do what you're doing. But contrived use-cases will occasionally (though rarely) exist.
The use-cases are never great, but are most compelling when you need Class features which are harder to write with prototypical inheritance (and of course since Classes are sort of wrappers around prototypical inheritance, your use case boils down to wanting the syntactic sugar plus es6+ class features... do you want constructors and super and extends and staticmethod etc.... or not? Are you stuck working with classes which already exist, or can you write everything using Objects and asserts? And does it really need to be anonymous? These questions may help you decide.).