I have recently got to know that we do have class expressions too like function expressions in JS. I know some points about it(MDN) like:
Class expressions may omit the class name ("binding identifier"), which is not possible with class statements.
Class expressions allow you to redefine (re-declare) classes without throwing a SyntaxError.
I understand what it can help in, but where should be use it? What can be a classical example of using a class expression that can't be achieved with class statements or vice-versa?
There aren't that many, but any time you're using
class
as a right-hand value, you're automatically using a class expression (just as withfunction
expressions).One place I've seen
class
expressions used is when wrapping classes:I'm not saying that's a good idea (I'd probably use a mixin, or composition rather than inheritance), but I've seen it done.
Something that looks like a
class
expression but is in fact more exotic than that is default exporting an unnamed class:I really don't like that one, because A) I don't like default exports, and B) I don't like unnamed functions/classes (other than really simple callback functions, for instance to
map
ornew Promise
).It's technically an anonymous
class
declaration (just as the equivalent would be an anonymous function declaration), but it's very similar to aclass
expression. (The default export case is the only place the specification allows anonymousclass
or function declarations.)If you were creating an object with several classes on it:
(I don't think I've ever done that, but...)