What is the best way to simulate protected variables in javascript?
class Parent{
#variable; // Private variable. This is not to be public.
constructor(value)
{
this.#variable = value;
}
}
class Child extends Parent{
getNewVariable()
{
return (1 + this.#variable); // Uncaught SyntaxError: Private field '#variable' must be declared in an enclosing class
// We want #variable to be accessible without making it public.
}
}
Since javascript doesn't support protected variables yet,
What is the best option for accessing private variables from superclasses in extended classes?
I haven't been able to find much documentation on this, so any suggestions would be very helpful.
get variable(){ return this.#variable; }For each variable is not ideal. However if this is the only option, then it will have to do. :)