JavaScript: code organization template

If not working with a framework, try the following:

$(function() {
  function MyFunction() {
  }

  MyFunction.prototype = {
    myMethod: function() {
    }
  };

  var myInstance = new MyFunction();
});

If you are looking for something simpler, kind of a one quick page deal:


var GLOBAL_NAMESPACE_HERE = (function () {
    var _private_obj_name_here = {
        // settings and defaults here
        key_here: value_here
    };

    /********************************************************************
     * Initialization:
     * Starts the listeners and get things rolling
    */
    _private_obj_name_here.init = function () {
    };

    /********************************************************************
     * Getters, other stuff
    */
    _private_obj_name_here.getSomething = function () {
        return _private_obj_name_here.key_here;
    };

    /********************************************************************
     * Expose public methods (the ones that can be called from outside)
    */
    return {
        init : _private_obj_name_here.init,
        getSomething: _private_obj_name_here.getSomething
    };

}());

This is called the module pattern.

GLOBAL_NAMESPACE_HERE.init();