Private class fields in JavaScript, this time in true sense!

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
Search for a command to run...

Co-Founder, @CreoWis | Teacher, @tapaScript | Founder, @ReactPlay | YouTuber | Writer | Human
No comments yet. Be the first to comment.
Traditional SSR is great, but it has a massive buffering problem. Let's look into the SSR Streaming and learn how it could be a game changer.

Learn how to build maintainable, reusable forms in React using design patterns like custom hooks, compound components, and context for scalable CRUD

Learn how JavaScript variables work, including let vs const, primitive and reference types, pass by value, and how JavaScript stores data in memory.

A beginner-friendly introduction to JavaScript covering what it is, how it works, and how to set up your JavaScript environment

Learn how the promises are handled internally using event loop. This concept is essentials to debug and work with async programming.

GreenRoots Blog - A Blog by Tapas Adhikary
186 posts
Educator | YouTuber | Building ReactPlay | Let's Connect
In general, one should ignore reading this further as, we are talking about yet another way to make a class field Private in JavaScript! We, the JS Developer community know more than couple of ways to do it already.
Be it,
WeakMapsSymbolsWith any of the methods listed above, nothing really comes as a direct offering from JavaScript Language. Some of these methodologies are not making the fields truly private, well, until now.
As part of the new class fields proposal, we are going to have the ability to declare a class field as Truly Private.
The Private fields can be enforced with the # as prefix to the variable intended to be Private. See the code block below:
class Counter {
#count = 0
increment() {
this.#count++
}
}
In above code, we have declared a class Counter with a private field count. You can actually run it on Latest Chrome(version # 72) or NodeJs(Version # 12).
Try accessing the count variable as:
new Counter().#count
This is the part amazed me and I have a great feeling of, JavaScript started behaving like other normal programming languages.
As quoted by the specification, here are the bundles of Joy!
Private fields provide a strong encapsulation boundary: It's impossible to access the private field from outside of the class, unless there is some explicit code to expose it (for example, providing a getter). This differs from JavaScript properties, which support various kinds of reflection and metaprogramming, and is instead analogous to mechanisms like closures and WeakMap, which don't provide access to their internals.
Yeah, you must have some questions about it by now. Basic question I had was,
private keyword? Isn't that more normal than # prefix?Well, they thought about it and please read this to gain more knowledge on it.
Let us welcome private to our life.
Please Share/Like if this information was useful to you. The Cover Picture credit goes to, Dayne Topkin on Unsplash