2017-03-18 1 views
-1

함수를 생성하면서 인수로 모든 인스턴스 변수를 추가하는 것을 잊어 버렸습니다. 그리고 그 함수는 상속받을 인수를 선택한다고 생각했기 때문에 필자는 필연적으로 생각했습니다. 그것들을 되풀이하기 위해서, 그들이 필요하고, 그렇다면 어떤 목적이 통과 되었는가?클래스에서 수퍼 함수로 인수 전달 : 필요합니까?

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 
+1

누락 된 인수가 표시되지 않습니다. 생성자가 세 개를 요구하면 세 개를 전달합니다. 내가 놓친 게 있니? – gyre

+0

"JavaScript 함수는 수신 된 인수의 수를 확인하지 않습니다." https://www.w3schools.com/js/js_function_parameters.asp – jyrkim

+0

나는 수퍼가 수퍼바이져와 동일한 인자를 전달받는 것을 보지만, 그것들 없이는 잘 작동하는 것 같다. 그래서 나는 그 목적을 이해하려고 노력하고있다. super 함수를 인수로 전달합니다 – Samatha

답변

0

인수를 전달해야합니다. 확장 클래스의 인스턴스를 만들지 않고 기본 클래스를 만들면 테스트가 올바르게 수행되지 않습니다. 마지막 라인을 변경하는 경우

, 어떻게 볼 :

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);

이러한 모든 특성은 이제 정의되지 않습니다.