Different ways of defining, but what's the difference?
class A {}
const B = class {}
const C = class {
static define() {
/* do some code */
return this // the class itself, as it is a static method
}
}.define()
I would expect the problem to be in B and C then A to just work, but A and B works fine, and C just doesn't.
When I log them, I get the classes. But if I log their name
property:
class A {}
const B = class {}
const C = class {
static define() {
/* do some code */
return this // the class itself, as it is a static method
}
}.define()
console.log('A -', A.name) //output: "A - A"
console.log('B -', B.name) //output: "B - B"
console.log('C -', C.name) //output: "C -"
I'm bit confused. If const
holds just the value (class), how does it give a name. Or if it's variable's name or something similar, why wouldn't it work with a returned value in C then?
I tried to research but couldn't find an article. Then I made tests. Results were unexpected (they're included in the question). I expect someone to explain what is going on here. It's a really subtle thing. But the class is treated like an anonymous class in one way, and the other is like a normal class, which I didn't expect to be. The classic way works just fine, but this, I really wonder this.