Bits and Pieces

Insightful articles, step-by-step tutorials, and the latest news on full-stack composable software development

Follow publication

Understanding Public and Private Fields in JavaScript Class

Chidume Nnamdi 🔥💻🎵🎮
Bits and Pieces
Published in
6 min readMar 25, 2019

--

Privacy

class Time {
private int secound;
private int minutes;
private int hour;
Time(int hour, int secound, int minutes) {
this.hour = hour;
this.secound = secound;
this.minutes = minutes;
}
public String buildString()
{
return String.format( "%24s: %s\n%24s: %s", "this.toUniversalString()", , "toUniversalString()", );
} // end method buildString
// convert to String in universal-time format (HH:MM:SS)
public String toUniversalString()
{
return String.format( "%02d:%02d:%02d", , , );
}
}
Time time = new Time(10, 12, 23);
time.hour = 2
TimeTest.java:9: hour has private access in Time
time.hour = 2; // error: hour has private access in Time
^
class Time {
private int secound;
private int minutes;
private int hour;
Time(int hour, int secound, int minutes) {
this.hour = hour;
this.secound = secound;
this.minutes = minutes;
}
// ...
public setHourToZero() {
this.hour = 0
}
}
class Time {
private int secound;
private int minutes;
private int hour;
Time(int hour, int secound, int minutes) {
this.hour = hour;
this.secound = secound;
this.minutes = minutes;
}
private formatTime() {
// ...
}
public trySomethingNew() {
this.formatTime()
}
//...
}
Time time = new Time(10, 1, 2);
time. trySomeThingNew()
time.formatTime()
TimeTest.java:9: formatTime has private access in Time
time.formatTime(); // error: method formatTime has private access in Time
^
class Time {
constructor(hour, minute, second) {
this.hour = hour
this.minute = minute
this.second = second
}
buildString() {
return String.format( "%24s: %s\n%24s: %s", "this.toUniversalString()", , "toUniversalString()", );
} // end method buildString
// convert to String in universal-time format (HH:MM:SS)
toUniversalString() {
return String.format( "%02d:%02d:%02d", , , );
}
}
const time = new Time(10, 2, 4)
time.hour = 20
time.minute = 10

The Proposal — Stage 3

Private class field

class Time {
#hour = 0
#minute = 0
#second = 0
constructor(hour, minute, second) {
this.#hour = hour
this.#minute = minute
this.#second = second
}
//...
}
const time = new Time(10, 2, 4)
time.#hour = 20 // will throw error #hour has a private access in Time
time.#minute = 10 // will throw error #minute has a private access in Time
class Time {
#hour = 0
#minute = 0
#second = 0
constructor(hour, minute, second) {
this.#hour = hour
this.#minute = minute
this.#second = second
}
#setZeroHour(int hour) {
this.#hour = hour
}
callSetZeroHour() {
this.#setZeroHour()
}
//...
}
const time = new Time(10, 2, 4)
time.#setZeroHour(0) // will throw error #setZeroHour has a private access in Time
const time = new Time(10, 2, 4)
time.callSetZeroHour()

Private static properties

class Time {
#hour = 0
#minute = 0
#second = 0
static #gmt = 0
constructor(hour, minute, second) {
this.#hour = hour
this.#minute = minute
this.#second = second
}
//...
}
const time = new Time(10, 2, 4);
Time.#gmt // it will throw, static property #gmt is inaccessible from outside the Time class.
class Time {
#hour = 0
#minute = 0
#second = 0
static #gmt = 0
constructor(hour, minute, second) {
this.#hour = hour
this.#minute = minute
this.#second = second
}
setGMT(int gmt) {
Time.#gmt = gmt
}
//...
}
const time = new Time(10, 2, 4)
time.setGMT(9)
class Time {
#hour = 0
#minute = 0
#second = 0
static #gmt = 0
constructor(hour, minute, second) {
this.#hour = hour
this.#minute = minute
this.#second = second
}
static displayHour() {
return Time.#getHour()
}
static #getHour() {
return this.#hour
}
//...
}
const time = new Time(10, 2, 4)
Time.#getHour() // Access denial error
Time.displayHour()

Conclusion

Build JS apps faster with reusable components

Ramda functions as reusable Bit components

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in Bits and Pieces

Insightful articles, step-by-step tutorials, and the latest news on full-stack composable software development

Written by Chidume Nnamdi 🔥💻🎵🎮

JS | Blockchain dev | Author of “Understanding JavaScript” and “Array Methods in JavaScript” - https://app.gumroad.com/chidumennamdi 📕

Responses (3)

Write a response