Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
Being able to call a static method as a fallback from an instance in javascript?

In python, you can call static/class methods from an instance, for example in the following:

class User:
    # class/static
    NUM_USERS = 0
    MAX_USERS = 10
    @classmethod
    def users_left(cls):
        print ('%d users left' % (cls.MAX_USERS - cls.NUM_USERS))
    # instance
    def __init__(self, name):
        self.name = name
        User.NUM_USERS += 1
    def greet(self):
        print ('Hello %s' % self.name)

u1 = User('Bob')
u2 = User('Todd')
u2.greet()
u2.users_left() # users_left() is a classmethod

However, in javascript it seems you must explicitly call the class, for example:

class User {
    static NUM_USERS = 0;
    static MAX_USERS = 10;
    static users_left() {
        console.log(User.MAX_USERS - User.NUM_USERS);
    }
    constructor(name) {
        this.name = name;
        User.NUM_USERS ++;
    }
    greet() {
        console.log('Hello', this.name);
    }
}

u1 = new User('Bob');
u2 = new User('Todd');
u2.greet();

u2.constructor.users_left(); // ok
User.users_left(); // ok
u2.users_left(); // error

Is there a way to 'fallback' in javascript like you can in python, or you must explicitly invoke the class?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!