# Allow \`this\` in static methods to access any members without exception

**URL:** <https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718>\
**Category:** 💡 Ideas\
**Created:** [June 1, 2023, 1:05am UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718 "2023-06-01T01:05:18Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![geryogam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/geryogam/32/1756_2.png) [@geryogam](https://es.discourse.group/u/geryogam)\
**Post date:** [June 1, 2023, 1:05am UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/1 "2023-06-01T01:05:18Z")

</div>

# Problem

From [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields#private_fields):

> There is a restriction on private static fields: only the class which defines the private static field can access the field. This can lead to unexpected behavior when using `this`. In the following example, `this` refers to the `Subclass` class (not the `ClassWithPrivateStaticField` class) when we try to call `Subclass.publicStaticMethod()`, and so causes a `TypeError`.
> 
> ```javascript
> class ClassWithPrivateStaticField {
> static #privateStaticField = 42;
> 
> static publicStaticMethod() {
> return this.#privateStaticField;
> }
> }
> 
> class Subclass extends ClassWithPrivateStaticField {}
> 
> Subclass.publicStaticMethod();
> // TypeError: Cannot read private member #privateStaticField from an
> // object whose class did not declare it
> 
> ```
> 
> […]
> 
> You are advised to always access private static fields through the class name, not through `this`, so inheritance doesn’t break the method.

So `this` cannot be used in static methods to access _private static members_ (the example for fields above also applies to methods) because it breaks for subclasses.

However `this` can be used in instance methods to access _private instance members_ (the example for fields below also applies to methods) because it works for subclass instances:

```javascript
class ClassWithPrivateInstanceField {
  #privateInstanceField = 42;

  publicInstanceMethod() {
    return this.#privateInstanceField;
  }
}

class Subclass extends ClassWithPrivateInstanceField {}

new Subclass().publicInstanceMethod(); // 42

```

The fact that `this` can be used in static methods to access any members, _except private static members_, has two drawbacks:

1. Information duplication by requiring to use the class name for the exception to the rule.
2. Bad developer experience by requiring to memorize the exception to the rule.

# Solution

Lift the restriction that forbids access to private static members through subclasses.

# Prior art

Python doesn’t have that restriction, so `this` can be used in static methods to access any members, _including private static members_ (prefixed with `__`):

```python
class ClassWithPrivateStaticField:
    __private_static_field = 42

    @classmethod
    def public_static_method(this):
        return this.__private_static_field

class Subclass(ClassWithPrivateStaticField):
    pass

Subclass.public_static_method() // returns 42

```

---

<div class="post-metadata">

**Author:** ![Josh-Cena](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/josh-cena/32/1408_2.png) [@Josh-Cena](https://es.discourse.group/u/Josh-Cena)\
**Post date:** [June 1, 2023, 5:18am UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/2 "2023-06-01T05:18:00Z")

</div>

Python doesn't have private properties per se (it merely has the underscore convention), so the example is inadequate as you can do the same in JS as well. C++/C#/Java doesn't allow using `this` in static methods and none of them allow accessing `Subclass.privateStaticFieldFromSuper` either. Not sure about others.

---

<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:** [June 1, 2023, 5:22am UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/3 "2023-06-01T05:22:52Z")

</div>

This restriction was explicitly decided upon due to the conflicting mental models of how inheritance should work for them, and is unlikely to ever be lifted. I can't locate the issue or the notes where it was discussed, but hopefully someone can reply with the relevant links.

---

<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:** [June 1, 2023, 5:30am UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/4 "2023-06-01T05:30:50Z")

</div>

There's also that afaik no other language has truly _private_ fields, since "accessible via reflection" is still public, so I'm not sure if there's truly equivalent prior art to look at.

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [June 1, 2023, 9:36am UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/5 "2023-06-01T09:36:25Z")

</div>

See also cross-post at [javascript - Why is access to private static members through a subclass forbidden? - Stack Overflow](https://stackoverflow.com/questions/75962400/why-is-access-to-private-static-members-through-a-subclass-forbidden)

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/senocular/32/642_2.png) [@senocular](https://es.discourse.group/u/senocular)\
**Post date:** [June 1, 2023, 1:46pm UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/6 "2023-06-01T13:46:23Z")

</div>

There was this issue in the proposal repo:

> <https://github.com/tc39/proposal-class-fields/issues/43>
>
> What is the output of:
> 
> \`\`\`js
> class Base {
> static #field = 'hello';
> 
> s…tatic get() {
> return this.#field;
> }
> }
> 
> class Sub extends Base {}
> 
> // This one isn't controversial
> Base.get() // =\> 'hello'
> 
> // But what does this do?
> Sub.get()
> \`\`\`
> 
> I want it to equal \`'hello'\`, but I can't find where the behavior is defined. It's likely just because I'm not familiar enough with the spec text.
> 
> Maybe it's an error, though? Since I don't think \`Sub\` has \`#field\` in its \`\[\[PrivateFieldValues\]\]\` list, unless there's some super class iteration I'm not seeing.

---

<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:** [June 1, 2023, 2:06pm UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/7 "2023-06-01T14:06:08Z")

</div>

> [@ljharb](#):
>
> There's also that afaik no other language has truly _private_ fields, since "accessible via reflection" is still public, so I'm not sure if there's truly equivalent prior art to look at.

Hmm, I don't think it's fair to discount languages that allow reflecting private fields. Yes, it does mean their implementation will be a little different, but unless those differences actually affected this specific decision, it still makes sense to consider what they were doing, why, and how it worked out for them.

> [@bergus](#):
>
> See also cross-post at [javascript - Why is access to private static members through a subclass forbidden? - Stack Overflow](https://stackoverflow.com/questions/75962400/why-is-access-to-private-static-members-through-a-subclass-forbidden)

I'm struggling to understand the arguments in here. It feels like the meat of the argument (in the one answer given) is that instance fields are created when the prototype relationship is already known while static fields can be created before anyone ever actually inherits from them.

But, I fail to see why that matters.

TBH - I never even realized you could use "this" inside of a static function - it makes sense after thinking about it for two seconds, it just never ocurred to me to do it, I've always used the class name. I'll have to start using "this" in static functions now... unless I'm accessing a private field and I expect inheritance relationships to be built from the class.

---

<div class="post-metadata">

**Author:** ![geryogam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/geryogam/32/1756_2.png) [@geryogam](https://es.discourse.group/u/geryogam)\
**Post date:** [June 1, 2023, 4:22pm UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/8 "2023-06-01T16:22:43Z")

</div>

> [@Josh-Cena](#):
>
> Python doesn't have private properties per se (it merely has the underscore convention), so the example is inadequate as you can do the same in JS as well.

Note that I was not referring to the [_single_ underscore prefix](https://docs.python.org/3/tutorial/classes.html#private-variables) (`_`) which indeed restricts access to private members only by _convention_ (no language-based access restriction).

I was referring to the [_double_ underscore prefix](https://docs.python.org/3/tutorial/classes.html#private-variables) (`__`) which restricts access to private members by _name mangling_ (language-based direct access restriction): a member `__m` of a class `C` is mangled to `_C __m` which prevents the _direct_ access `this.__ m` (but the _indirect_ access `this._C__m` is still possible).

So both strategies are limited forms of access restriction, but the latter is _language-based_ like the hash prefix `#` in JavaScript so they are similar in that regard (though access restriction in JavaScript is stronger).

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [June 1, 2023, 5:58pm UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/9 "2023-06-01T17:58:40Z")

</div>

[GitHub - tc39/proposal-class-access-expressions: ECMAScript class access expressions](https://github.com/tc39/proposal-class-access-expressions) feels like a nice solution to this issue.

`this.#foo` becomes `class.#foo`

---

<div class="post-metadata">

**Author:** ![geryogam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/geryogam/32/1756_2.png) [@geryogam](https://es.discourse.group/u/geryogam)\
**Post date:** [June 1, 2023, 7:08pm UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/10 "2023-06-01T19:08:46Z")

</div>

> [@ljharb](#):
>
> This restriction was explicitly decided upon due to the conflicting mental models of how inheritance should work for them, and is unlikely to ever be lifted.

It seems conflicting only for implementers, but for mere users like me it’s the current behaviour which is unexpected. I expect the member accesses `C.m` (or `this.m`) and `C.#m` (or `this.#m`) to walk the prototype chain for any static members; I don’t expect a special restriction for private static members.

The privacy of a member should only restrict the _scopes_ from which it can be accessed (i.e. inherited), not restrict the access (i.e. inheritance) completely: `m` is public so `C.m` (or `this.m`) is allowed in any scope, whereas `#m` is private so `C.#m` (or `this.#m`) is allowed only in the scope of the class that defines `#m`. That’s what Python subtly does as shown in the original post and what JavaScript currently doesn’t.

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [June 1, 2023, 8:04pm UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/11 "2023-06-01T20:04:10Z")

</div>

> [@geryogam](#):
>
> I expect the member accesses `C.m` (or `this.m`) and `C.#m` (or `this.#m`) to walk the prototype chain for any static members; I don’t expect a special restriction for private static members.

That does seems like a more confusing behaviour to me as a user. Why should static members be treated differently than instance members? No private member access walks the prototype chain, that's a simple rule to memorise. There is no extra "restriction" as you call it, this is just the normal behaviour.

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [June 1, 2023, 8:08pm UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/12 "2023-06-01T20:08:54Z")

</div>

> [@theScottyJam](#):
>
> I'll have to start using "this" in static functions now... unless I'm accessing a private field and I expect inheritance relationships to be built from the class.

I would be cautious with that. Especially if you don't expect other classes to inherit from yours, you should generally not use `this` but always refer to your class directly by name. _Only_ if you expect derived classes, and if you want them to be able override the methods you are calling, you should use `this`.

---

<div class="post-metadata">

**Author:** ![geryogam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/geryogam/32/1756_2.png) [@geryogam](https://es.discourse.group/u/geryogam)\
**Post date:** [June 1, 2023, 8:59pm UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/13 "2023-06-01T20:59:15Z")

</div>

> [@aclaymore](#):
>
> `this.#foo` becomes `class.#foo`

`class` would indeed be a nice addition to the language (Python also has it, it’s named ` __class__ `). But `class` can’t replace `this` in general because `this` allows the member to be overridden in a subclass whereas `class` doesn’t, so the use case is slightly different. Static private members can’t be overridden so in that case both `class` and `this` should be equivalent (in Python they are equivalent in that case), but that’s not what currently happens since `this` doesn’t work in that case. So `class` is a different feature that happens to coincide with `this` for static private members, but that doesn’t mean `this` shouldn’t be fixed. Users just want to be able to use `this` uniformly (i.e. for any members: public or private).

---

<div class="post-metadata">

**Author:** ![geryogam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/geryogam/32/1756_2.png) [@geryogam](https://es.discourse.group/u/geryogam)\
**Post date:** [June 1, 2023, 9:18pm UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/14 "2023-06-01T21:18:09Z")

</div>

> [@bergus](#):
>
> Why should static members be treated differently than instance members? No private member access walks the prototype chain, that's a simple rule to memorise.

That’s exactly that rule that I’m questioning in this thread. _Privacy_ should only prevent inheritance (i.e. member access) _outside_ of the scope of the class defining the member; it should not prevent inheritance _completely_ (i.e. including in the scope of the class defining the member).

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [June 1, 2023, 9:39pm UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/15 "2023-06-01T21:39:11Z")

</div>

Hm, but wouldn't that be confusing as well?

```js
class Example {
   #priv = 1;
   static {
      const e = new Example();
      console.log(e.#priv); // normal, works as expected
      const clone = Object.create(e);
      console.log(clone.#priv); // what should this do?
      clone.#priv = 2; // should this work as well?
      ({}).#priv = 3; // and if yes, what about this?
   }
}

```

(using private members of an `Example` instance here, but it's the same for private static members)

I mean, I would really have loved if private names were just privately scoped and would otherwise work like any other property, on arbitrary objects (raising questions about bracket syntax and proxies though), but that's not what the committee decided on. While I don't like it either, it's at least consistent. Whether it's too late to change without breaking the web, I cannot assess.

---

<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:** [June 1, 2023, 10:41pm UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/16 "2023-06-01T22:41:28Z")

</div>

The primary difference here is that private instance fields are all installed at instantiation time - iow, either when a class is constructed, or when something extended it is constructed and invokes it via `super()`. The inheritance chain is already set up - and whatever the chain is at `new` time is how private fields become installed.

A private static field, however, is installed at the time the class is _created_ - which is _before_ any subclasses have had a chance to extend it. There's either no code there to invoke, and so no way to install it on a subclass at extension time - or, there is code there to invoke, and an object value will be duplicated - or, the value is assigned from the superclass.

In the former case, you have a hazard/bug farm (that already exists for objects stored on the prototype, but that's not easy to do with class syntax, by design). In the latter case, you have a surprising inconsistency with instance fields, which _are_ re-evaluated every time.

If we were to re-evaluate a static instance field, then you'd be able to observe/hook into "extension", which is not a current capability nor one that has consensus to exist.

The issue doesn't occur for public static fields because public fields, unlike private fields, DO walk up the prototype chain.

Hopefully that clears it up a bit.

---

<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:** [June 2, 2023, 12:28am UTC](https://es.discourse.group/t/allow-this-in-static-methods-to-access-any-members-without-exception/1718/17 "2023-06-02T00:28:16Z")

</div>

> [@bergus](#):
>
> I would be cautious with that. Especially if you don't expect other classes to inherit from yours, you should generally not use `this` but always refer to your class directly by name. _Only_ if you expect derived classes, and if you want them to be able override the methods you are calling, you should use `this`.

Yeah - I mostly was talking about internal only classes, i.e. "I don't expect others to inherit" == "this class is only used in my own code and is not publicly exported, so I don't have to worry about random users inheriting it".

Then again, maybe I won't do it. It could be confusing following a double standard like that, as opposed to just always using the class names.
