我正在尝试通过将类方法的返回值传递给Javascript类中的变量来初始化它,如下所示:
class Agent{
constructor(batteryCapacity){
this.currentDate = this.getDate();
this.ethereumAddress = this.getAccount();
this.balance = this.getAgentBalance();
}
getDate(){
day = currentDate.getDate();
month = currentDate.getMonth() + 1;
year = currentDate.getFullYear();
//this.currentDate = day + "/" + month + "/" + year;
//Can I assign the variable within this method?
return day + "/" + month + "/" + year;
}
async getAccount(index) {
accounts = await web3.eth.getAccounts();
return accounts[index];
}
async getAgentBalance() {
this.balance = await web3.eth.getBalance(ethereumAddress);
return balance;
}
我收到错误:未定义currentDate。但是,我觉得我正在用getDate()的返回值从字面上定义它,所以为什么会出现此错误? 另外,我是否能够像我在该函数的注释部分中所述的那样,在方法函数内将变量分配给类,并简单地调用函数this.getDate()并在其中分配this.currentDate?
在这方面的任何帮助将不胜感激。