Posts Tagged ‘JavaScript Singleton jQuery’

I am a firm believer of not having wayward functions cluttering up my JavaScript code. Often it is needed to encapsulate these in a globally accessible object. I use this pattern. Note we lean on jQuery to initialize the object when the DOM is ready.

var controller = {
  init: function () {
    controller.wirePageEvents();
  },
  wirePageEvents: function () {
    // Init events
  },
  method1: function () { },
  method2: function () { }
}
$(document).ready(function() {
  controller.init();
});
Advertisements