DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 964,423 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
VICTOR OMONDI
VICTOR OMONDI

Posted on

Static methods in Python vs Javascript

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()
Enter fullscreen mode Exit fullscreen mode

Javascript

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

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

User.whatAmI();
Enter fullscreen mode Exit fullscreen mode

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 πŸ“£βœ”οΈβœ”οΈβœ”οΈ

Top comments (1)

Classic DEV Post from 2020:

js visualized

πŸš€βš™οΈ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! πŸ₯³

Happy coding!