I am learning module pattern in JavaScript. I started with a simple example given in Mozilla Documentation for a closure, which is as below:
function makeFunc() {
var name = "Mozilla";
function displayName() {
alert(name);
}
return displayName;
}
var myFunc = makeFunc();
myFunc();
Now I created a fake widget utilizing the module pattern. I am not sure if it abides by the module pattern's convention but looks like below:
var MyWidget = MyWidget || {};
MyWidget.Feature = (function() {
function someDataPlusBehavior() {
var a = {
name: "AJ",
age: 32,
giveFullIntroduction: function() {
alert(this.name);
alert(this.age);
}
};
return a; //returns the data, settings, behavior etc as an object
}
return {
init: function() {
var aFeature = someDataPlusBehavior();
var myData = aFeature.name;
alert("My Name is:" + myName);
alert("My Full Intro:" + aFeature.giveFullIntroduction());
}
};
})();
MyWidget.Feature.init();
How else could I have made any better usage of closure in this example? Could I have made use of someDataPlusBehavior
function differently in the closure function init
?