DEV Community

Discussion on: ECMAScript 2021 New Updates

Collapse
 
baenencalin profile image
Calin Baenen

Can you have public counterparts to private properties?
E.g.

class MyClass {
  #priv;
  get priv() {
    return this.priv;
  }
}
Enter fullscreen mode Exit fullscreen mode

Or is JS to primitive to handle it keeping its logic simple?

Collapse
 
animeshmaru profile image
Animesh

Yes, we can have public getter function to get a private properties.

Collapse
 
baenencalin profile image
Calin Baenen

I'm specifically referring to them sharing the same name, so the getter can let you do .priv without just giving you (direct) access to .priv.

Thread Thread
 
animeshmaru profile image
Animesh • Edited

Ok, your example is correct. In new update now we can make getter function also private so that it can only be accessed inside the class, but priv in your ex can be get from outside.