我有一个基类:
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
最佳答案
为什么使用 lodash 来扩展 ES6 类?不能只使用 extends
关键字吗?
class Derived extends Base {
constructor() {
super();
this.name = "derived"
}
getDerivedName() {
return this.name;
}
}
关于Javascript - 使用 lodAsh 扩展时,基类方法在派生类中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30243206/