loading...

Static methods in Python vs Javascript

victoromondi1997 profile image VICTOR OMONDI ・1 min read

Python

class User():
    def __init__(self, username):
        self.username = username

    @staticmethod
    def what_am_i():
        return 'I can be called by User class and also by any User instance'

User.what_am_i()

Javascript

class User {
    constructor(username) {
        this.username = username;
    }

    static whatAmI() {
        return 'I can be called only by User class'
    }
}

User.whatAmI();

Thing to note

In python if we declare a static method it can be called by both the class and by any of the instances (objects) created from the class ✔️✔️✔️

Whereas in Javascript though, if we declare any static methods, it can only be accessed by calling it from the class and not by its instances 📣✔️✔️✔️

Discussion

pic
Editor guide