# Now is the best moment to implement protected fields in classes

**URL:** <https://es.discourse.group/t/now-is-the-best-moment-to-implement-protected-fields-in-classes/1593>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [January 15, 2023, 10:56pm UTC](https://es.discourse.group/t/now-is-the-best-moment-to-implement-protected-fields-in-classes/1593 "2023-01-15T22:56:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArthurLobopro](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/arthurlobopro/32/1616_2.png) [@ArthurLobopro](https://es.discourse.group/u/ArthurLobopro)\
**Post date:** [January 15, 2023, 10:56pm UTC](https://es.discourse.group/t/now-is-the-best-moment-to-implement-protected-fields-in-classes/1593/1 "2023-01-15T22:56:01Z")

</div>

The ECMAScript 2022 brought support to private fields in classes using `#` but still without support to protected fields (still acessible internaly in subclasses but not externaly). This resource can be very useful and already is implemented (declaration only) in typescript.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [January 15, 2023, 11:36pm UTC](https://es.discourse.group/t/now-is-the-best-moment-to-implement-protected-fields-in-classes/1593/2 "2023-01-15T23:36:55Z")

</div>

That's because, as has been discussed extensively on other threads, "protected" doesn't make any sense in JS. JS doesn't have "access levels", it has "reachability" - things are either reachable or aren't.

I'd recommend searching this discourse for other discussions on the subject.

---

<div class="post-metadata">

**Author:** ![mhofman](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mhofman](https://es.discourse.group/u/mhofman)\
**Post date:** [January 16, 2023, 3:48am UTC](https://es.discourse.group/t/now-is-the-best-moment-to-implement-protected-fields-in-classes/1593/3 "2023-01-16T03:48:32Z")

</div>

One of those thread where it is postulated that there may be a way to implement protected like semantics: [A simple way to implement protected](https://es.discourse.group/t/a-simple-way-to-implement-protected/1110)

---

<div class="post-metadata">

**Author:** ![ArthurLobopro](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/arthurlobopro/32/1616_2.png) [@ArthurLobopro](https://es.discourse.group/u/ArthurLobopro)\
**Post date:** [January 30, 2023, 2:49pm UTC](https://es.discourse.group/t/now-is-the-best-moment-to-implement-protected-fields-in-classes/1593/4 "2023-01-30T14:49:00Z")

</div>

Thanks for the tip, but for very large classes it is not feasible to do all this manually, a native resource would be very useful in these cases. If you see the typescript, it already implements this feature in the typing which shows that there is a use for it

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [January 30, 2023, 3:00pm UTC](https://es.discourse.group/t/now-is-the-best-moment-to-implement-protected-fields-in-classes/1593/5 "2023-01-30T15:00:40Z")

</div>

No, it shows that there's an expectation that "access levels" from other languages should work in JS - but the existence of an expectation isn't the same thing as the expectation being reasonable.

---

<div class="post-metadata">

**Author:** ![mhofman](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mhofman](https://es.discourse.group/u/mhofman)\
**Post date:** [January 30, 2023, 3:29pm UTC](https://es.discourse.group/t/now-is-the-best-moment-to-implement-protected-fields-in-classes/1593/6 "2023-01-30T15:29:44Z")

</div>

> [@ArthurLobopro](#):
>
> Thanks for the tip, but for very large classes it is not feasible to do all this manually

From what I recall it's mostly a matter of defining the right helpers and/or decorators.

---

<div class="post-metadata">

**Author:** ![rdking](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rdking/32/54_2.png) [@rdking](https://es.discourse.group/u/rdking)\
**Post date:** [February 1, 2023, 5:07pm UTC](https://es.discourse.group/t/now-is-the-best-moment-to-implement-protected-fields-in-classes/1593/7 "2023-02-01T17:07:19Z")

</div>

Instead of thinking in terms of "access levels" (a hill you're willing to die on to prevent), try thinking in terms of an opt-in mechanism for seamlessly sharing private fields with descendants. That's what I think most of us want when we ask for "protected fields". It's not even necessary for it to be called "protected" if the concept rubs you the wrong way so harshly. All we really want is the functionality. Since it can be done in runtime logic with ES as it currently stands, there shouldn't be any logical issue with providing it. If there is, I'd like to know what that is.
