Skip to content

Instantly share code, notes, and snippets.

@gorhgorh
Created June 16, 2013 09:29
Show Gist options
  • Save gorhgorh/5791528 to your computer and use it in GitHub Desktop.
Save gorhgorh/5791528 to your computer and use it in GitHub Desktop.
/* jshint forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true,
* strict:true, undef:true, curly:true, browser:true, indent:2, maxerr:50
*/
/*
* Note: use http://www.jshint.com/ to validate your code and make sure it
* doesn't catch any bad practices or error prone programming, also it may
* be handy to leave the options you validated your code with at the top
* of the file so it can be easiy validated later
*/
/*
* Note: Local variables of global properties are specified here, this way we
* can at a glance see which external objects / libraries are being used in the
* object. Think of it as an informal module export. These are the local
* variable names that will be used in your script. At the bottom of the
* anonymous function is immediately invoked with the global names as params
* at the very bottom.
*/
(function(z, $, console) {
/*
* Note: Use strict is within the IIFE so that it only applies to this code
* block. within the file use strict prevents common errors and bad practices
* by catching them at runtime. The statement is removed by google closure at
* compilation time.
*/
'use strict';
/**
* Description of what the widget does
*
* @example
* z.ExampleObject(selector, {
* width:100,
* height:50
* }); // This should be an example of how use the class
*
* @constructor
* @param {String/Object} selector Matches one element, create multiple
* instances for multipleelements or if you feel you need to match multiple
* elements attempt to use the jQuery plugin pattern.
*
* @param {Object} settings This is an example parameter description.
* @param {Integer} settings.width This is a description of the items within
* the settings object.
* @param {Integer} settings.height This is a description of the items within
* the settings object.
* @param {String} settings.title This is a description of the items within
* the settings object.
*
* @return {Type} description of the return value (if any).
*/
z.ExampleObject = function ExampleObject(options) {
// if it was not declared with the "new" syntax
if (!(this instanceof z.ExampleObject)) {
// make sure that the object is instantiated
return new z.ExampleObject(options);
}
/*
* Note: Declare a variable to reference 'this', this is useful when your
* context switching many people use this, its handy but totally optional.
*/
var self = this;
// Provide default settings
var defaultSettings = {
/* NOTE: these are example default settings */
width: 300,
height: 100,
title: 'My Object'
};
// Merge default settings with the constructor options
self.settings = $.extend({}, defaultSettings, options);
// If your object / object methods are meant to be chainable, notate it
// explicitly here:
return this;
};
/**
* Note: Public methods should be documented well with JSDoc, and placed
* before all private methods
*/
/**
* Displays the object on the website
*
* @example
* var exObj = new z.ExampleObject(options);
* // Delay display of object by half a second.
* setTimeout(function () {
* exObj.render();
* }, 500);
*
*/
z.ExampleObject.prototype.render = function render() {
};
/* Note: private methods do not to be excessivly documented in JSDoc */
/*
* Note: private methods are prefixed with an underscore to notate that the
* method typically should not be used outside of the object.
*/
/*
* Note: reasoning for hungarian notated private methods is that using the var
* statement to or a regular function declaration, the variable scope is
* shared between multiple instances which could cause errors.
*/
/**
* Attaches events to the elements relating to the object.
*/
z.ExampleObject.prototype._attachEvents = function _attachEvents() {
};
/*
* Note: Shows which global objects are being used. The local definitions of
* these global variables are specified above the IIFE
*/
}(window.z, window.jQuery, window.console));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment