Return to Snippet

Revision: 11770
at February 17, 2009 18:02 by johnloy


Initial Code
var myModule = function () {
	
	var publicObj = {}; // returnable object...

	var privateProperty = "I can be accessed only from within this module";
	var privateMethod = function () {
		// has access to public properties as publicObj.publicVar and private methods
	}

	var publicObj.publicProperty = "Publicly accessible as myModule.publicVar.";
	var publicObj.publicMethod = function () { 
		// Publicly accessible as myModule.publicMethod	
	}

	return publicObj;

}();


var myModule = function(){

	var privateMethod = function(){
		publicMethod();
	}

	var publicMethod = function(){
		return this;
	}

	// Copy over methods you want to be public
	return {
		publicMethod : publicMethod
	}

}();


var myModule = function(){

	// Persists in privateMethods due to closure
	var that = {};

	function privateMethod(){
		that.publicMethod();
	}

	return {
		publicProperty: true,
		publicMethod: function(){},
		// Make this visible in private scope
		init: function(){
			that = this;
		}
	};
	
}().init();

Initial URL


Initial Description


Initial Title
variations of the Javascript module pattern

Initial Tags
javascript, textmate

Initial Language
Other