In this blog,
we will cover private class features
which will be a part of ECMAScript 2022.
By default, all properties in ES6 classes are public
and can be examined or modified outside the class.
In the example below,
we find that there is nothing in the class
to prevent the property from being changed without calling the setter.
Starting from ECMAScript 2022,
private class fields are defined using a hash # prefix,
From the above example,
we can modify it to include a private class field
to prevent the property from being changed outside of the class method.
When we try to modify the private class field outside of the setter method,
we get the error -
We can also make methods or getter/setters private,
by giving them names starting with #.
Private class features are under development or implemented in below -