I have a class, let's call it CoolClass which I am using all over my code to access it's static properties. However, I have a setup step within CoolClass that I define as a function as part of my class; let's call it doSetupStuff(). To get that to fire I call it via setting a static dummy variable.
This is my current solution which is working:
export default class CoolClass {
public static readonly something = 'cool';
public static readonly somethingelse = 12345;
// GOAL: how can we call doSomeStuff() only once, without setting this 'dummy' variable?
public static readonly dummy = CoolClass.doSetupStuff();
public static doSetupStuff(): void {
console.log('doing setup...');
// more logic here...
}
}
But this feels incorrect to me because I never actually need CoolClass.dummy - I just need to run what is in doSetupStuff().
I know I can do this:
export default class CoolClass {
public static readonly something = 'cool';
public static readonly somethingelse = 12345;
constructor() {
this.doSetupStuff();
}
public doSetupStuff(): void {
console.log('doing setup...');
// more logic here...
}
}
but then I can't use CoolClass statically. (i.e. I have to call new every time I want to use it in other parts of my code).
Surely there is no need to set a dummy variable like in my current solution. Does anyone have an idea? I can post the full code if needed, though I believe it is irrelevant to the pattern I wish to achieve.
export default class A {...} A.setup();