Protected has not been given to us, w3C tries to make internal APIs that aren't internal

The web spec authors have made an API that is supposedly "internal" but actually isn't. Looks like a "protected" feature was needed:

How do you recommend solving that problem?

Another issue there is that we have no way to guarantee that something runs post-constructor (synchronously, immediately after a new call).

If we had that ability, as discussed here, then it would be conceivable that web specs could define an API like attachInternals that is enforced to be available only during construction, essentially making it "protected" for all constructor codes in a hierarchy.

I've responded in that issue.

Another issue there is that we have no way to guarantee that something runs post-constructor (synchronously, immediately after a new call).

Yeah, this is something I've wanted too (for reasons unrelated to privacy) - it's reasonably common for a base class to want to perform some initialization after subclass constructors run, and that's awkward right now.

The pattern I currently use for this is to have a finalize method, which throws if called more than once, and make it part of the class contract that the base class and every subclass must have if (new.target === [this class]) this.finalize() as the last line of the constructor. And any subclasses which want to add more finalization have to put super.finalize() in their own finalize implementation.

This is a little bit annoying - in particular, it's annoying that nothing enforces that subclasses remember to do this - but I don't know if it's bad enough to warrant a feature in the language.

1 Like

If it is something people will forget, because it isn't enforced, then I think that's a good reason; reduction of human error.