是否可以在JavaScript中模拟抽象基类?最优雅的方式是什么?
说,我想做一些事情: -
var cat = new Animal('cat');
var dog = new Animal('dog');
cat.say();
dog.say();
Run Code Online (Sandbox Code Playgroud)
应输出:'bark','meow'
我想使用 super 的构造函数将子类实例传递给超类,但出现此错误
super(this);
Run Code Online (Sandbox Code Playgroud)
这在超类构造函数调用之前是不允许的
为什么我收到这个错误,我该如何解决这个问题
class Parent
{
constructor(child)
{
this.child = child;
}
//...somewhere in code
//child.doSomething();
}
class Child extends Parent
{
constructor()
{
super(this); // <==== the error here
}
doSomething = () =>
{
//...
}
}
Run Code Online (Sandbox Code Playgroud)