2

The ecmascript candidate spec allows to declare class fields like:

class A {
    foo;
}

or with value assignment like:

class A {
    foo = 'abc';
}

Public instance fields spec on MDN

Is there any way to reflect the list of declared fields names (and assigned value) from the class declaration in similar way how we are able to reflect class methods ? :

class B {
    foo = 'abc';
    boo() {}
}
Object.getOwnPropertyNames(B.prototype) // => ["constructor", "boo"]
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
majo
  • 381
  • 3
  • 8

1 Answers1

0

From what I've searched, you have to create an instance of the class itself to access the values of variables inside the scope.

So you can always list the default values of the constructor if you do the following:

class B {
    foo = 'abc';
    boo() {}
}
Object.getOwnPropertyNames(new B) // => ["foo"]

Hope that helps solving your question

nabais
  • 1,981
  • 1
  • 12
  • 18
  • I know that it is easy task with instance, but looking for solution without, directly from **declaration**. – majo Sep 22 '20 at 13:20
  • Then no, the properties don't exist until an object constructs them. You can see here for more detail: https://stackoverflow.com/questions/30518711/get-the-public-properties-of-a-class-without-creating-an-instance-of-it – nabais Sep 22 '20 at 13:33