2017-10-24 3 views
0

私のスーパーメソッドは年齢変数に未定義を返しますが、私はなぜそれがわかりません。スーパーメソッドは、Es6で定義された変数に対して未定義を返す

はここに私のコードです:

class customer_info{ 

    constructor(name, age = 50, gender){ 
     this.name = 'Lambo'; 
     this.age = age; 
     this.gender = 'Male'; 
    } 

    getCustomerInfo(){ 
     let customer_list = {Names: this.name, Age: this.age, Gender: this.gender}; 
     return customer_list; 
    } 

} 

let cust = new customer_info('Micheal Lambo', 49, "male"); 

class account_details extends customer_info { 

    constructor(account_no, account_name, initial_deposit){ 
     super(name, age); 
     this.account_no = account_no; 
     this.account_name = account_name; 
     this.initial_deposit = initial_deposit; 
    } 

    deposit(){ 

    } 

    withdrawal(){ 

    } 

    balance(){ 

    } 

    getCustomerAccount(){ 
     return super.getCustomerInfo(); 
    } 

} 

let cust_acct = new account_details(); 
cust_acct.getCustomerAccount(); 
+0

;'、あなたがそれから来て、 'name'のと' age'値を期待しています'super()'に渡しますか?あなたのコードがここに示しているように、あなたが表示するコードに 'name'も' age'も定義されていない変数なので、エラーが発生します。コンストラクタでは、第1引数または第3引数を使用せず、第2引数はデフォルト(デフォルト)を持っているため、代わりに 'super()'を実行すれば、おそらくコードが機能するでしょう。値。 – jfriend00

+0

しかし 'constructor(name、age = 50、gender)'を定義し、 'name'や' gender'引数をまったく使用しないのは間違ったコードでしょう。 'name'と' gender'引数を渡すので 'customer_info'オブジェクトを使う人は混乱しますが、あなたの実装は' name'を 'Lambo'に、' gender'を '男性 '。 – jfriend00

+0

[もっと速い回答を得るために、どのような状況で私の質問に「緊急」や他の類似のフレーズを追加することができますか?](// meta.stackoverflow.com/q/326569) - 要約は、これはボランティアに対処する理想的な方法であり、おそらく回答を得ることは非生産的です。これをあなたの質問に追加しないでください。 – halfer

答えて

2

名前と年齢が定義されていないこのコンストラクタ

class account_details extends customer_info{ 
    constructor(account_no, account_name, initial_deposit){ 
     super(name, age); 
     this.account_no = account_no; 
     this.account_name = account_name; 
     this.initial_deposit = initial_deposit; 
    } 

では、コンストラクタのパラメータとして渡します。その後、

class account_details extends customer_info{ 
    constructor(name, age, account_no, account_name, initial_deposit){ 
     super(name, age); 
     this.account_no = account_no; 
     this.account_name = account_name; 
     this.initial_deposit = initial_deposit; 
    } 

そして、このようにそれを呼び出す:あなたは `スーパー(名前、年齢)行うと

let cust_acct = new account_details('name', 32, account_no, account_name, initial_deposit); 
関連する問題