The instance of the class.
The name of the static property to retrieve.
The value of the static property.
This function retrieves a static property from the class of the given instance. It traverses the prototype chain to find the property in the class constructor. If the property is not found, it throws an error.
class MyClass {
static myStaticProperty = "Hello, world!";
static get myProperty() {
return this.myStaticProperty;
}
}
const instance = new MyClass();
const value = getStatic(instance, "myStaticProperty");
console.log(value); // "Hello, world!"
const method = getStatic(instance, "myProperty");
console.log(method); // "Hello, world!"
const notFound = getStatic(instance, "nonExistent"); // Error: Static property "nonExistent" not found in prototype chain.
Get a static property from a class instance.