0

In the calling of a static method within a class, what is the difference between class.staticFunction vs this.constructor.staticFunction. For example:

class User{
    /**
    * @param {string} first_name
    * @param {string} last_name
    * @param {number} age
    */
    constructor(first_name, last_name, age){
        this.first_name = first_name;
        this.last_name = last_name;
        this.age = age;
    }

    canVote(){
        //This
        return this.constructor.getVotingAge()<=this.age
        // Or This
        return User.getVotingAge()<=this.age
    }

    static getVotingAge(){
        return 18
    }
}

//sample usage
const jennifer = new User("Jennifer", "Doe", 20);
console.log(User.getVotingAge());
console.log(jennifer.canVote());

And what is better to use, and why?

0 Answers0