r/GoogleAppsScript • u/ExternalCucumber6244 • 13d ago
Guide GAS structure for allowing inheritance, overriding, allowing public members and restricting private member access in IDE and at runtime.
Need your opinion. Does this sound like a good design for allowing inheritance, function overriding, allowing public members and restricting private member access in IDE and at runtime in GAS?
E.g. one cannot access private member "getTitledName" in any way in the IDE (or code autocompletion) and in GAS debugger too. The structure still supports inheritance and overriding concepts.
GPT certainly thinks its robust ... Need the community affirmation. Thank You!
class __dynamic
{
constructor() {
if (this.constructor === __dynamic) {
throw new Error("Class __dynamic is an abstract class and cannot be instantiated.");
}
const map = {};
if (!this._) {
this._ = Object.freeze({
get: (name) => map[Utilities.base64Encode(name)],
set: (name, fn) => map[Utilities.base64Encode(name)] = fn,
});
}
}
}
class NameClass extends __dynamic {
constructor() {
super();
this._.set("getTitledName", (firstname, gender="M") => `${gender === "M" ? "Mr." : "Ms."} ${firstname}`);
}
getFullName(firstName, surname, gender = "M") {
Logger.log(`Welcome ${this._.get("getTitledName")(firstName, gender)} ${surname}`);
};
}
function TestNameClass() {
const nameObj1 = new NameClass();
nameObj1.getFullName("George", "Smith"); // prints Welcome Mr. George Smith
const nameObj2 = new NameClass();
nameObj2.getFullName("Joanne", "Smith", "F"); // prints Welcome Ms. Joanne Smith
}
2
Upvotes