【发布时间】:2020-09-25 09:57:14
【问题描述】:
我正在尝试从模块内的另一个类扩展一个类。代码如下:
let af = {
MyClass: class {
constructor() {
console.log("constructor of my class");
}
},
myNextClass: class extends this.MyClass { // *
constructor() {
console.log("constructor of the next class");
}
},
myOtherClass: class extends this.MyClass {
constructor() {
console.log("constructor of the other class");
}
},
}
在结果控制台中抛出 TypeError:
Uncaught TypeError: class heritage this.MyClass is not an object or null 指的是*行。你能帮我解决这个问题吗?
【问题讨论】:
-
不,不是m/M不一致,只是这里的错字,很抱歉(已编辑)
-
如果需要使用
this,请不要使用箭头函数。 -
@Barmar 这是非常好的建议,谢谢!但不幸的是,它并没有解决我的问题。
-
您不能在方法调用之外使用
this。创建对象时没有可用的this。
标签: javascript class inheritance ecmascript-6