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?