class B {
foo() {
return A.foo(); // message: Uncaught ReferenceError: A is not defined
}
}
class A {
static {
console.log(new B().foo());
}
static foo() {
return "foo";
}
}
I expected the "foo" to be called, but I can't even access A class. Isn't the A class declared when the static block works?
According to MDN, static blocks...
These blocks are evaluated during the initialization of the class, and therefore, the class is not initialized, nor is it defined yet; hence the error.
So in conclusion, the behavior you are seeing is expected. That also explains why moving the static block down (after the
foomethod) does not fix the error, and also whythis.foo()works butnew B().foo()does not,... given this (also from MDN).