Search code examples
javascriptclassprototypeproto

Why people[1].a don't have access to properties from class User?


Why people[1].a don't have access to properties from class User, but have need nested from through proto ?!

class User {
        constructor(name, age) {
            this.name = name;
            this.age = age;
        }

        static a = 12;
    }

    class User2 extends User {}

    let people = [
        new User2('Vasia', 20),
        new User2('Peter', 19),
        new User2('Misha', 21),
    ]



    console.dir(User2.a) // 12
    console.dir(people[1].a) // ?????

Solution

  • static properties appear on the Class objects. They do not appear on instances of the class.

    They are intended to hold static methods.

    From MDN:

    The static keyword defines a static method for a class. Static methods aren't called on instances of the class. Instead, they're called on the class itself. These are often utility functions, such as functions to create or clone objects.