tc39 / proposal-class-fields

Orthogonally-informed combination of public and private fields proposals
https://arai-a.github.io/ecma262-compare/?pr=1668
1.72k stars 113 forks source link

Another proposal to the private syntax #294

Closed hktonylee closed 4 years ago

hktonylee commented 4 years ago

Not sure if anyone has proposed that before. I would suggest this syntax rather than the terrible # prefix in case we really need private in JavaScript.

class SomeClass {
  private { // private keyword defines a companion object for SomeClass
    myPrivateField: 5;
    myPrivateFunc() {
    }
  }
  myPublicFunc() {
    private.myPrivateFunc(); // private is a special operator too to access the private companion object
    return private.myPrivateField;
  }
}
ljharb commented 4 years ago

Please see the FAQ for why that syntax doesn't work (including that private fields must be able to be accessed on any object, not just this).

hktonylee commented 4 years ago

Thanks I will close this issue then.