Return to Snippet

Revision: 18983
at October 13, 2009 20:14 by peta


Initial Code
// WAY 1 - Instantiating an anonymous function
var MySingleton = new (new MyClass(args));
 
// WAY 2 - Using an instance manager
var getMySingleton = (function(options) {
	function MyClass(args) {}

	var singleton;
	return function(args) {
		return singleton ? singleton : singleton = new MyClass(args);
	}
})();
 
// WAY 3 - The simple way
var MySingleton = {
	prop: "foo",
	method: function() {},
	...
};

Initial URL

                                

Initial Description

                                

Initial Title
3 ways of implementing the Singleton pattern in JavaScript

Initial Tags

                                

Initial Language
JavaScript