Skip to content

Instantly share code, notes, and snippets.

@keithelliott
keithelliott / example_namespace_usage.js
Created July 25, 2012 19:26
use a javascript namespace
//uses the following syntax to create an object with private properties
// and functions. You expose the properties and functions you need publicly in the
// return object.... The immediate function allows us to have private scope for
// certain things
// Create a new namespace
CHATHAM.namespace('CHATHAM.utils.gridmgr');
CHATHAM.utils.gridmgr = (function(){
@keithelliott
keithelliott / namespace.js
Created July 25, 2012 19:03
Creating javascript namespaces
// Chatham Namespace for exposing javascript functionality
// We can expose all of our functionality via this one global var
var CHATHAM = CHATHAM || {};
CHATHAM.namespace = function (namespace_name_string) {
var parts = namespace_name_string.split('.'),
parent = CHATHAM,
i, cnt;
// remove the first part of the string which will be 'CHATHAM'
function hello(name) {
this.name = name;
this.sayIt = function(callback) {
var self = this;
setTimeout(function() {
callback("Hello " + self.name);
}, Math.random()*1000);
};
};
==============================================================
MODULE :: hello.js
==============================================================
module.exports = function hello(name) {
this.name = name;
this.sayIt = function() {
return "Hello " + this.name;
};
function getQuerystringParamByName(name) {
name = name.replace(/[\[]/, '\\\[').replace(/[\]]/, '\\\]');
var regexS = '[\\?&]' + name + '=([^&#]*)'
, regex = new RegExp(regexS)
, results = regex.exec(window.location.search);
if(!results) {
return '';
}
function getTimeSince(start, end) {
var msSince = (end - start);
var msInDay = 24*60*60*1000
, msInHour = 60*60*1000
, msInMinute = 60*1000
, msInSecond = 1000;
if (msSince > msInDay) { // greater than one day
return parseInt(msSince / msInDay).toString() + 'd Ago';