# Lightweight Protected Fields

**URL:** <https://es.discourse.group/t/lightweight-protected-fields/1102>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [November 21, 2021, 9:28am UTC](https://es.discourse.group/t/lightweight-protected-fields/1102 "2021-11-21T09:28:37Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [November 21, 2021, 4:32pm UTC](https://es.discourse.group/t/lightweight-protected-fields/1102/6 "2021-11-21T16:32:51Z")

</div>

This is a well thought-out proposal. We have actually discussed protected fields in the past, but failed to come up with a way to actually implement them in JavaScript (see [here](https://es.discourse.group/t/protected-and-friend-access-modifier-for-class-members/732/3), and I just found [this thread](https://es.discourse.group/t/protected-support-for-class/68/23) as well while looking for the other one).

One of the things we got stuck on in the previous thread, was how to actually implement protected. I think you're getting really close to something. Bouncing off of your ideas, and a few of my own thoughts, I think it would be possible to create protected logic.

Take this code example:

```javascript
class BaseClass {
  #x
  constructor(x) {
    this.#x = x
  }

  protected #getSecretValue() {
    return this.#x
  }
}

class SubClass extends BaseClass {
  logSecretValue() {
    console.log(super.#getSecretValue())
  }
}

```

This can be implemented, behind the scenes, as follows. `[[homeObject]]` represents the homeObject hidden field, and `[[protectedFields]]` is a new hidden field that contains a mapping of hidden field names to values.

```javascript
class BaseClass {
  #x
  constructor(x) {
    this.#x = x
  }

  protected #getSecretValue() {
    return this.#x
  }

  [[hiddenFields]]: new Map([
    ['getSecretValue', this.#getSecretValue]
  ])
}

class SubClass extends BaseClass {
  logSecretValue() {
    console.log(super[[hiddenFields]].get('getSecretValue').call(this))
  }
}

```

There's a couple of restrictions with how this machinery works. First, it's only designed to work with methods. Secondly, when you access a super-class protected field, you must also call it (thus, making it impossible to, for example, .call() the protected field with another value). I bet there are ways to lift these restrictions, but I think this alone would be a good starting point that many people would be happy with.

As a side note - I like the syntax of using `super.#whatever()` to access protected properties. I think it looks clean to have protected access look different from private access.

* * *

With all of this said, I'm actually not the biggest fan of the protected access modifiers. It's something I've been doing a lot of researching and learning about recently, and I've come to believe that there's always a better way to structure one's code, than to use a protected-enabled class, which means, I also believe that adding protected to the language would encourage people to write worse code. If interested, there's [an ongoing discussion](https://es.discourse.group/t/the-role-of-inheritance-in-javascripts-future/919/29) about inheritance in general, that doesn't (yet) talk about protected directly, but does discuss items closely related, and the overall value of protected fields would be an on-topic discussion point over there. Either way, I just wanted to quickly express this opinion, but I don't want to pollute this thread with a discussion around it.

---

_[View the full topic](https://es.discourse.group/t/lightweight-protected-fields/1102)._
