为什么我的遗产不起作用?

时间:2017-09-08 00:24:08

标签: javascript inheritance

LivingThing.call无法正常工作。在对象创建上创建的唯一属性是description属性。 - 为什么?我多次检查过这个问题,但有些东西一直让我不知所措。

    function LivingThing(name,sex,distance,attributes,health,level=1){
        this.name=name;
        this.sex=sex;
        this.distance= distance;
        this.attributes=attributes;
        this.health=health;
        this.level= level;
    }
    function Animal(name,sex,distance,description,attributes,health,level){
        LivingThing.call(name,sex,distance,attributes,health,level);
        this.description= description;
    }
    Animal.prototype=Object.create(LivingThing.prototype);
    Animal.prototype.constructor= Animal;

1 个答案:

答案 0 :(得分:3)

您忘记将上下文参数传递给LivingThing

function Animal(name,sex,distance,description,attributes,health,level){
    LivingThing.call(this,name,sex,distance,attributes,health,level);
    this.description= description;
}