Javascript 将类方法返回值传递给构造函数中的类变量

Javascript 将类方法返回值传递给构造函数中的类变量,javascript,class,constructor,Javascript,Class,Constructor,我试图通过从类方法传递一个返回值来初始化Javascript类中的变量,如下所示: class Agent{ constructor(batteryCapacity){ this.currentDate = this.getDate(); this.ethereumAddress = this.getAccount(); this.balance = this.getAgentBalance(); } getDate()

我试图通过从类方法传递一个返回值来初始化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


如果您在这件事上有任何帮助,我们将不胜感激。

这是
getDate()
方法中未定义的
currentDate
。如果我没弄错的话,你的
getDate()
实际上是
getCurrentDate()
并且应该是
static
使用
this.currentDate
它是
currentDate.getDate(),而不是InstanceHank you haim770的
.currentDate
属性,我错过了这个细节,现在它可以工作了,但是如果我在getDate()函数定义之前使用static,我会得到这个。getDate()不是一个函数。为什么你认为它需要是静态的?