我有一个基类:

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 - Javascript-使用lodAsh扩展时,派生类中不提供基类方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30243206/

10-09 18:25
查看更多