call a static property on an instance of a class in javascript

To call a static property on an instance of a class in JavaScript, you need to use the class name followed by the dot (.) notation and the property name.

Here's an example:

index.tsx
class MyClass {
  static myProperty = "Hello World";
}

const myInstance = new MyClass();

console.log(MyClass.myProperty); // "Hello World"
console.log(myInstance.myProperty); // undefined
190 chars
9 lines

In the code above, we define a class called MyClass with a static property called myProperty. We then create an instance of the class called myInstance.

To call the myProperty on the class itself, we use the class name MyClass followed by the dot notation and the property name myProperty. This will output "Hello World" to the console.

However, when we try to call the myProperty on an instance of the class, such as myInstance.myProperty, we get undefined. This is because static properties belong to the class itself, not its instances.

gistlibby LogSnag