在 类 中的超级函数中传递参数:是否需要?
Passing arguments in super function in classes: is it needed?
我正在创建一个函数,但忘记将所有实例变量添加为参数,但它工作得很好,我认为这是必然的,因为我认为你正在选择要继承的参数,但它似乎在没有它的情况下工作,所以重申一下,它们是否必要,如果是,它们传递的目的是什么,谢谢
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
您需要传递参数。您的测试未正确完成,因为您没有创建扩展 class 的实例,而是创建基础 class.
的实例
如果更改最后一行,看看会发生什么:
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 HouseCat("spots",4,"female"); // <--- !!!!!!!
console.log(spots);
所有这些属性现在都未定义。
我正在创建一个函数,但忘记将所有实例变量添加为参数,但它工作得很好,我认为这是必然的,因为我认为你正在选择要继承的参数,但它似乎在没有它的情况下工作,所以重申一下,它们是否必要,如果是,它们传递的目的是什么,谢谢
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
您需要传递参数。您的测试未正确完成,因为您没有创建扩展 class 的实例,而是创建基础 class.
的实例如果更改最后一行,看看会发生什么:
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 HouseCat("spots",4,"female"); // <--- !!!!!!!
console.log(spots);
所有这些属性现在都未定义。