JavaScript中类和子类表达式的对象
JavaScript中有没有办法制作一个对象,其中包含和子类的表达式?我尝试过这种方式,但我想我不能以这种方式参考超级阶级。
const myClasses = {
'animal': class {
a;
b;
constructor(x, y) {
this.a = x;
this.a = y;
}
},
'dog': class extends animal {
constructor(d, e) {
super(d, e);
}
bark () {
console.log('woof!');
}
}
}
Is there a way in Javascript to make an Object containing expressions of both classes and subclasses of it? I tried this way, but I guess I can't refer to the superclass this way.
const myClasses = {
'animal': class {
a;
b;
constructor(x, y) {
this.a = x;
this.a = y;
}
},
'dog': class extends animal {
constructor(d, e) {
super(d, e);
}
bark () {
console.log('woof!');
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来OP的目的是建立一个可以根据键实例化的类索引。怎么样,按照往常来定义类,然后在索引中参考它们...
It looks like the OP is aiming for an index of classes that can be instantiated based on a key. How about, define the classes as you normally would, then refer to them in the index...
感觉有些怪异,但是解决方案是将其分为多个步骤。如果声明了超级类,则可以在第二步中提及。
一种方法,我仍然想知道。
It feels a bit weird, but a solution to this was to split it up into multiple steps. If the super class is declared, it can be referred to in a second step, obviously.
If there is a way to do it in one step, I'd still like to know.