Namespaces in Javascript

posted in: Uncategorized | 0

johnWeeGo.jpgWith so much code be written in javascript for modern web2.0 sites it is important to start using namespaces to ensure your code can won’t conflict with other functions and varibles. This is a nice technique that also allows for private varibles and private methods.


var namespacename = function() {
	var private_var;
	function private_method() {
		// do stuff here
	}
	return {
		method_1 : function() {
			// do stuff here
		},
		method_2 : function(something) {
			// do stuff here
		}
	};
}();

Now you can call namespacename.method_1(); or namespacename.method_2(foo); from your page or call private_method(); from within method_1 or method_2.