Adding ##foo for protected class fields?

10 views
Skip to first unread message

Alex Vincent

unread,
Apr 16, 2023, 1:17:20 PM4/16/23
to SES Strategy
We've had private class fields for a couple years now, and I love them.  I've also been using TypeScript for the past year, and I rather like its `protected` feature.

So, I have been wondering about merging the two ideas:  adding in syntax for protected class fields, via a double-hash prefix.

I can't attend SES meetings in the near future (new job, yay!), so I'll have to rely on this Google Groups mailing list for conversation.

Alex
--
"The first step in confirming there is a bug in someone else's work is confirming there are no bugs in your own."
-- Alexander J. Vincent, June 30, 2001

Mathieu Hofman

unread,
Apr 16, 2023, 1:50:56 PM4/16/23
to Alex Vincent, SES Strategy
The problem with protected is the question of its semantics in JavaScript. Private fields are explained in terms of a binding valid only in the scope of the class declaration. Would a protected field give access to that binding to any derived class? What happens in case where the prototype of the class gets changed after declaration? Should 2 classes from the same base class be able to access each other's protected field declared in the base class?

There are some discussions on TC39 ES Discourse where we show that a variant of protected is possible to implement on top of private fields in user land, and I still believe it can be made ergonomic by using class decorators.

Mathieu

--
You received this message because you are subscribed to the Google Groups "SES-strategy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ses-strategy...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ses-strategy/CAEZ8440OE9MzJHAsrq%3DLR60q40gstYHqnezQJPNLtb-jsMk1Hg%40mail.gmail.com.

Justin Ridgewell

unread,
Apr 25, 2023, 5:04:46 PM4/25/23
to SES-strategy
This is one of the goals of private declarations. While not real `protected`, it allows a sharing a private field between multiple classes.

Jordan Harband

unread,
Apr 26, 2023, 1:39:07 PM4/26/23
to Justin Ridgewell, SES-strategy
One of the benefits of using private declarations for this is that unlike “access levels” and “protected”, this would give you the same benefits with a mental model of scoping and reachability that actually matches the language. 

Reply all
Reply to author
Forward
0 new messages