Javascript Closures
I thought I’d take a moment to share my Javascript Closure template which I use regularly. Its a great way to namespace your functions and encapsulate functionality into packages. This is another helpful resource if you want to learn more about why we do it.
ProTip: If you’re a Visual Studio developer, this code snippet may save you a little time. Just change the suffix to .snippet and store in your code snippets folder.
var Namespace = function () {
return {
Init: function () {
//if anything needs to be inited, it can be done here
},
PublicFunction: function (args) {
//Need fully qualify function when calling
//a from within the namespace
Namespace.AnotherPublicFunction(args);
//fully qualified not needed for private functions
_privateFunction(args);
},
AnotherPublicFunction: function(args){
}
};
function _privateFunction : function(args){
}
} ();