Skip to content

Instantly share code, notes, and snippets.

@auramo
Last active August 29, 2015 14:13
Show Gist options
  • Save auramo/7cac7f6056939ddc86a6 to your computer and use it in GitHub Desktop.
Save auramo/7cac7f6056939ddc86a6 to your computer and use it in GitHub Desktop.
var myUtil = (function() {
function doStuffToDate(date) {}
...
return { doStuffToDate: doStuffToDate, //other public funcs }
})();
myUtil.doStuffToDate(date); //use
var dstd = myUtil.doStuffToDate; //"import"
//You can also "inline" the implementations if they're short
var myUtil = (function() {
function somePrivateFuncNotVisibleToOutSide() {...}
...
return { doStuffToDate: function(date) {...},
//other public funcs
}
})();
//"dotted namespace"
var my = {}
my.util = (function() {
function somePrivateFuncNotVisibleToOutSide() {...}
...
return { doStuffToDate: function(date) {...},
//other public funcs
}
})();
// ^ Beware of defining my twice if you have my.something in many places
// You can protect against that with this ugliness:
if (typeof(my) === 'undefined') {
var my = {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment