我有一个基类:
class Base {
constructor() {
this.name = "base_class"
}
getBaseName() {
return "base"
}
}
和派生类
var _ = require('lodash');
class Derived {
constructor() {
this.name = "derived"
}
getDerivedName() {
return "derived"
}
}
_.extend(Derived.prototype, Base)
我希望派生类中有getBaseName
可用。但事实并非如此。我该怎么办?我错过了什么?
var derived = new Derived();
console.log(derived.getBaseName)
-- undefined
答案 0 :(得分:2)
为什么使用lodash来扩展ES6类?你不能只使用extends
关键字吗?
class Derived extends Base {
constructor() {
super();
this.name = "derived"
}
getDerivedName() {
return this.name;
}
}