I'm having this code in JavaScript
class StringFormatter {
init(locale) {
this.locale = locale;
}
getA() {
return 'A' + this.locale
}
getB() {
return 'B' + this.locale
}
getC() {
return 'C' + this.locale
}
}
// when page just render
let stringManager = new StringFormatter();
stringManager.init('enUS');
// call it in other components
let result = stringManager.getA();
so when I'm using this OOP class, I only need to set the locale once by init it when page first render. And then all of the later code where I call methods getA, getB or getC will adapt to this locale.
Now as I want to move this to a more Functional Programming way, how can I write it but still keep the same concept. Right now I can only come up with this solution
getA(locale) {
return 'A' + locale
}
getB(locale) {
return 'B' + locale
}
getC(locale) {
return 'C' + locale
}
let result = getA('enUS');
But this one required me to keep passing the locale every time I call the function getA, getB or getC.
Is there anyway that we somehow still can config the locale but only once, don't need to pass it every time to getA, getB or getC function, and don't need to write in OOP way?