How is it possible to have a _subset_ of the private fields of a class?

Hi.

The current specification says (ECMAScript® 2024 Language Specification):

because field initializers can throw during class instantiation, an individual object may have some proper subset of the private fields of a given class

I'm wondering if that's correct. My understanding is if a field initializer throws, the instantiation fails and so the instance never becomes available. Am I missing something?

A reference to the instance is accessible in the field initialisers that have run before the construction fails.

function error() { throw new Error() }

new class {
  #a;
  #b = console.log(#a in this, #b in this);
  #c = error();
}
// logs: true, false
1 Like

Got it, thanks!

1 Like