Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 ?

share|improve this question
3  
This might be better suited for codereview.stackexchange.com –  Blazemonger Jan 30 at 23:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.