I'm a Ruby on Rails and React Native enthusiast with over four years of experience creating innovative software solutions. I work well in teams and am always up for challenging projects. I enjoy playing chess and gaming in my free time.
2 minute read
Private methods proposal, as a part of
the class fields proposal
has been promoted to Stage 4 in
the TC39 process
and will be officially included in ECMAScript 2022!
In this blog,
we will cover private class features
which will be a part of ECMAScript 2022.
Before
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.
After
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 -