JmJ
JmJ

Reputation: 2098

Is it possible to access class properties without initialising the class?

I want to access all of the properties of a class that will be defined when the constructor is called, so that I can implement a sort of interface for the class.

Say I had a class that defines the property hello, I would like to access it to check it has been implemented and the type assigned to it is correct. Problem is, since all non-static class properties are tied to an instance, I can't get at them without instantiating the class, which I can't do.

In this situation, is it possible to access hello?

class MyClass {
    constructor () {
        this.hello = 'greetings';
    }
}

Upvotes: 0

Views: 242

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074999

In this situation, is it possible to access hello?

Not without using a JavaScript parser (like IDEs do to try to infer instance mbmers). hello, as you say, doesn't exist as a property until/unless an instance is created. With a parser, you can (usually) determine what the property names will be, perhaps sometimes their initial values, but that's all.

Upvotes: 3

Related Questions