✋ Exploring the concept of access modifiers from TypeScript, how can we make it relevant for JavaScript developers as well? Let's consider a scenario where a parent class defines necessary members and a shared method:
// ParentClass.js
export default class ParentClass {
private static voo;
private bar;
constructor(bar) {
this.bar = bar;
}
private commonMethod(baz) {
doSomething(ParentClass.voo);
doSomethingElse(this.bar, baz);
}
}
Now, let's look at a child class that inherits this behavior:
// ChildClass.js
export default class ChildClass extends ParentClass {
voo = "Jake";
constructor(bar) {
super(bar)
}
public uniqueMethod(ter) {
doAnotherThing(this.bar);
this.commonMethod(ter);
}
}
When calling commonMethod()
within ChildClass.uniqueMethod()
, it references the value of ParentClass.voo
, which is undefined. How can we ensure that each inheriting child class uses the same implementation of the method but references the static member of the child class itself? Is there a solution available for this issue?
One workaround would be to make voo
an instance member instead of a static member, but if a static voo
is more useful in certain scenarios, what other solutions are possible?
I've shared my current solution in response to this question, but I am curious if there is a more direct approach to address this challenge.