# A simple way to implement protected

**URL:** <https://es.discourse.group/t/a-simple-way-to-implement-protected/1110>\
**Category:** 💡 Ideas\
**Created:** [November 26, 2021, 8:43pm UTC](https://es.discourse.group/t/a-simple-way-to-implement-protected/1110 "2021-11-26T20:43:14Z")\
**Posts on this page:** 1\
**Showing post:** 28

<div class="post-metadata">

**Author:** ![trusktr](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/trusktr/32/183_2.png) [@trusktr](https://es.discourse.group/u/trusktr)\
**Post date:** [February 23, 2022, 7:09pm UTC](https://es.discourse.group/t/a-simple-way-to-implement-protected/1110/28 "2022-02-23T19:09:59Z")

</div>

> [@theScottyJam](#):
>
> If you needed to access the property outside the class, then it's not really private to begin with

I was only accessing the property inside the class, and the property is supposed to be private. Believe me, I would have marked the variable as just `foo` to mark it as public if I was using it outside the class. :)

See here for a problem example:

> [@Why can't \`#private\` fields return undefined before initialization?](https://es.discourse.group/t/why-cant-private-fields-return-undefined-before-initialization/1227/2):
>
> Private fields don't have a temporal dead zone, they simply don't exist on the instance until base class constructor finishes executing. They can't be installed before that point because the base constructor is the thing which creates the object in the first place. As a general rule, you should avoid calling derived methods in base class constructors. Derived methods can (as in this case) rely on state which the derived class constructor sets up, and that hasn't happened when the base class con…

* * *

* * *

Random fun, but I've implemented "friends" or similar features here: [https://github.com/trusktr/lowclass/blob/dbb10b0c97a6690aabf4549c46a12baa8bd19ed5/src/Class.ts](https://github.com/trusktr/lowclass/blob/dbb10b0c97a6690aabf4549c46a12baa8bd19ed5/src/Class.ts)

Unit tests for that here: [https://github.com/trusktr/lowclass/blob/dbb10b0c97a6690aabf4549c46a12baa8bd19ed5/src/tests/friends.test.js](https://github.com/trusktr/lowclass/blob/dbb10b0c97a6690aabf4549c46a12baa8bd19ed5/src/tests/friends.test.js)

---

_[View the full topic](https://es.discourse.group/t/a-simple-way-to-implement-protected/1110)._
