DEV Community


Posted on

"Pseudo-Private" Properties in javaScript

"Pseudo-Private" Properties in javaScript

The addition of private fields and properties is relatively new

  • the old way description:

It's a quite common convention to prefix private properties with an underscore (_) to signal that they should not be accessed from outside of the object.
but  It's just a convention, It does NOT technically prevent access. You CAN run this code without errors,

  • the new way description:

 The( #) is a part of the name itself. Private fields are accessible on the class constructor from inside the class declaration itself. They are used for declaration of field names as well as for accessing a field's value.

so syntax error to refer to # names from out of scope. It is also a syntax error to refer to private fields that were not declared before they were called.

important: (maybe your IDE will complain about this # syntax but it works in chrome well).

Discussion (0)