You can call a static function that initializes all static members immediately after the class definition, and then further removes this function. (Perhaps there will be a function to reset static variables?)
This will allow you to save all your static variables inside the class declaration.
class C { static init() { C.alist = []; } constructor(x) {…} add(x) {…} show() {…} } C.init(); delete C.init;
Another option is to initialize static variables in the constructor, but this requires that at least one object be created before using static variables.
class C { constructor(x) { C.alist = C.alist || []; … } add(x) {…} show() {…} }
source share