Sometimes, we want to make a "public static field" in a JavaScript class.
In this article, we’ll look at how to make a "public static field" in a JavaScript class.
How to make a "public static field" in a JavaScript class?
To make a "public static field" in a JavaScript class, we can create a static getter.
For instance, we write
class Agent {
static get CIRCLE() {
return 1;
}
static get SQUARE() {
return 2;
}
}
console.log(Agent.CIRCLE);
to define the CIRCLE and SQUARE getters in the Agent class.
Then we access them like Agent.CIRCLE.
Conclusion
To make a "public static field" in a JavaScript class, we can create a static getter.