在Es6中为超定义变量返回undefined的超级方法

时间:2017-10-24 13:25:44

标签: javascript ecmascript-6

我的超级方法返回未定义的年龄变量,我不知道为什么。

这是我的代码:

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

1 个答案:

答案 0 :(得分: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);