Javascript 在类中的超函数中传递参数:是否需要?

Javascript 在类中的超函数中传递参数:是否需要?,javascript,class,inheritance,super,Javascript,Class,Inheritance,Super,我创建了一个函数,忘记了将所有实例变量作为参数添加进去,它工作得很好,我认为这是必要的,因为我认为你选择了什么参数来继承它,但它似乎工作得很好,所以要重申,它们是必要的吗?如果是,它们是为了什么目的而传递的 class Felidea{ constructor(name,age,sex){ this.name=name; this.age=age; this.sex=sex; this.hasRetractableClaws

我创建了一个函数,忘记了将所有实例变量作为参数添加进去,它工作得很好,我认为这是必要的,因为我认为你选择了什么参数来继承它,但它似乎工作得很好,所以要重申,它们是必要的吗?如果是,它们是为了什么目的而传递的

class Felidea{
    constructor(name,age,sex){
        this.name=name;
        this.age=age;
        this.sex=sex;
        this.hasRetractableClaws=true;
        this.hasNightVision=true;  //instance variables
    }
    static isSameSex(cat1,cat2){
        return cat1.sex===cat2.sex;
    }
    scratch(){
        console.log(this.name + ": scratch scratch scratch");
    }
    bite(){
        console.log(this.name + ": bite bite bite");
    }

}

class HouseCat extends Felidea{
    constructor(name,age,sex){
        super(); //arguements missing, I commonly see this have the same  properties as the parent class
        //super(name,age,sex,hasRetractableClaws,hasNightVision) this is what I commonly see
    }
    purr(){
        console.log(this.name + ": purr purr purr");
    }
}

 let spots= new Felidea("spots",4,"female"); // works fine and inherits the      
                                             //missing arguements varibles

你需要传递参数。测试没有正确完成,因为您没有创建扩展类的实例,而是创建基类

如果更改最后一行,请查看发生的情况:

Felidea类{ 建造师(姓名、年龄、性别){ this.name=name; 这个。年龄=年龄; 这个。性=性; 这个。hasRetractableFlaws=true; this.hasNightVision=true;//实例变量 } 静态isSameSex(cat1、cat2){ 返回cat1.sex==cat2.sex; } 刮擦{ console.log(this.name+“:scratch”); } 咬{ console.log(this.name+“:bite”); } } 家猫类{ 建造师(姓名、年龄、性别){ super();//缺少参数,我通常看到它与父类具有相同的属性 //super(姓名、年龄、性别、可伸缩的爪子、夜视)这是我经常看到的 } purr(){ console.log(this.name+“:purr purr purr”); } }
let spots=新家猫(“spots”,4,“雌性”);//我看不出缺少任何论据;你的构造器要求三个,你通过三个。我遗漏了什么吗?“JavaScript函数不检查收到的参数数量。”我通常看到super会获得与构造函数相同的参数,但如果没有这些参数,它似乎工作得很好,因此我试图理解将这些参数传递给super函数的目的,但您正在创建一个新的
Felidea
,不是新的
HouseCat
,因此
super
不参与您的“测试”。