Javascript - 使用lodAsh扩展时派生类中不可用的基类方法

时间:2015-05-14 17:07:31

标签: javascript ecmascript-6 lodash

我有一个基类:

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

1 个答案:

答案 0 :(得分:2)

为什么使用lodash来扩展ES6类?你不能只使用extends关键字吗?

class Derived extends Base {
    constructor() {
        super();
        this.name = "derived"
    }
    getDerivedName() {
        return this.name;
    }
}