gpt4 book ai didi

javascript - super 方法为 Es6 中定义的变量返回未定义

转载 作者:行者123 更新时间:2023-12-01 02:51:54 25 4
gpt4 key购买 nike

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

这是我的代码:

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

最佳答案

在此构造函数中

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

关于javascript - super 方法为 Es6 中定义的变量返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46911788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com