Skip to content

Node.js 12: Private Class Fields

Last updated on January 30, 2021

Javascript allows us to declare fields inside the class. We can access its own fields or properties by creating an instance of a class.

Example:

 Class Devreto {
   postId = 1;
}
const devreto = new Devreto();
devreto.postId ; //1 

As specified as of 2019, We can now declare private fields using #. Using private fields the implementation details of a class are kept internal and are not exposed to the world.

Example:

Class Devreto {
   #postId = 1;
}
const devreto = new Devreto();
devreto.postId ;  //undefined 
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments