JavaScript Module Template

RSS
Modified on 2012/03/21 16:57 by Mark Categorized as javascript, patterns
// Wrap code with module pattern.
(function(window, document /*, $ */, undefined) {
  var global = this;

  // add logging stubs
  if (!window.console) console = {};
  console.log = console.log || function () { };
  console.warn = console.warn || function () { };
  console.error = console.error || function () { };
  console.info = console.info || function () { };


  // helper functions
  var helperFunc = function(msg) {
    return msg + " helper";
  };

  // widget constructor function
  global.MaSl_CreateMyWidget = function() {

    // private instance methods    
    var privateFunc = function() { return null; }

    // public instance methods
    return {
      methodOne: function() {return "one" + privateFunc();},
      methodTwo: function(msg) {return "two " + msg;},
      methodThree : function(msg) {return "three " + msg;}
    };

  };

})(window, document /*, jQuery*//*no param for undefined*/);

See Also