Consider the following contrived bit of JavaScript based on the revealing module pattern:
var RevealingModuleSample = function () {
var moduleName = "DefaultModuleName",
init = function (name) {
moduleName = name
},
issueAlert = function () {
alert(moduleName + " module is running");
}
return {
init: init,
issueAlert: issueAlert
};
} ();
RevealingModuleSample.init("SampleModule");
RevealingModuleSample.issueAlert();
Is there a way to transform the moduleName into a property with getter and setter methods and have those getter/setter methods called by internal references to moduleName? To give a concrete example, consider the following similarly contrived C# example:
public class ContrivedCsharpExample
{
private string _moduleName;
protected string moduleName
{
get {
// Perhaps some logging code here that records the moduleName was retrieved.
return this._moduleName;
}
set {
// Some code here that ensure that a valid moduleName was passed.
this._moduleName = value;
}
}
public void init(string moduleName) {
this.moduleName = moduleName;
}
public void issueAlert() {
Console.WriteLine(this.moduleName);
}
public static void Main()
{
var module = new ContrivedCsharpExample();
module.init("SampleC#Module");
module.issueAlert();
}
}
Notice that internally to the class, the methods are calling properties that have a getter/setter. This allows us to add additional code inside those getter/setter methods, whereas if we only used the properties available outside the class, the "init" and "issueAlert" methods would have to directly inspect the field, and any logic we had added to the getter/setter would be bypassed.
The problem I'm seeing is that in the revealing module pattern, the "moduleName" variable isn't a property on an object--it's just a variable that's currently in scope. So you can't just yank it out and replace it with a get/set pair. Right?
There is a related question on StackOverflow, but in that case the questioner is looking for a way to return an object with a property with getter/setter code, so that outside users of the module have their property access routed through the get/set methods. A solution is listed right in the question, but the solution provided doesn't allow code that is inside the module to access that property without directly operating on the variable, bypassing the get/set functions.
I'm thinking that this simply isn't possible with the revealing module pattern, is that correct? Is there a way to have calls inside the module routed through the get/set functions?