Javascript Singleton Object Examples

Singleton object is an unique object that once instantiated, it can no longer be instantiated again, the resource of the object will be shared by the entire application that contains this object. As an example, for an object contains configuration information for of an application, it’s better to use the same object and the same resource. It is wasting time and resources to create this object every time when the application needs to grab some config info. In javascript, there is a lot of ways to create a Singleton object.

1. Object literals, the simplest form of javascript singleton object. All javascript object literals are singleton objects.

var dbconfig1 = 
{
	host : "localhost",
	user : "ken",
	password : "123456789",
	dbname : "ecom",
	isConnected : false,
	getStatus : function()
					{
						return (this.isConnected) ? "Connected to database!" : "Not connected to database";
					}
}
dbconfig1.isConnected=true;
console.log("dbconfig1: "+dbconfig1.getStatus()); //dbconfig1: Connected to database! 

2. Create an object and override the object variable by instantiating a new object of itself.It becomes a singleton object because you no longer can create the object again since the object variable is already overridden.

var dbconfig2 = function()
{
	this.host = "localhost";
	this.user = "ken";
	this.password = "123456789";
	this.dbname = "ecom";
	this.isConnected = false;

	this.getStatus = function()
	{
		return (this.isConnected) ? "Connected to database!" : "Not connected to database";
	}
}
dbconfig2 = new dbconfig2();
console.log("dbconfig2: "+dbconfig2.getStatus()); //dbconfig2: Not connected to database
var temconfig = new dbconfig2(); //This line will give you the error: Uncaught TypeError: object is not a function 

3. Self instantiating function, the key word new inistantiates the object on the fly thus made it a singleton object.

var dbconfig3 = new function()
{
	this.host = "localhost";
	this.user = "ken";
	this.password = "123456789";
	this.dbname = "ecom";
	this.isConnected = false;

	this.getStatus = function()
	{
		return (this.isConnected) ? "Connected to database!" : "Not connected to database";
	}
};
console.log("dbconfig3: "+dbconfig3.getStatus()); //dbconfig3: Not connected to database
var tempconfig = new dbconfig3(); //This line will give you the error: Uncaught TypeError: object is not a function

4. Another self instantiating function, instead of using the key word new, it attaches a pair of parenthese after the end of function declartion.

var dbconfig4 = function()
{
	this.host = "localhost";
	this.user = "ken";
	this.password = "123456789";
	this.dbname = "ecom";
	this.isConnected = false;

	this.getStatus = function()
	{
		return (this.isConnected) ? "Connected to database!" : "Not connected to database";
	}
	return this;
}();
console.log("dbconfig4: "+dbconfig4.getStatus()); //dbconfig4: Not connected to database
var temp = new dbconfig4(); //This line will give you the error: Uncaught TypeError: object is not a function

5. Making it a singleton function by returning the object itself if it is already instantiated.

var dbconfig5 = function()
{	
	// If the instance is object, return the object itself
	if (typeof(dbconfig5.instance) === 'object') {
        return dbconfig5.instance;
    }
	this.host = "localhost";
	this.user = "ken";
	this.password = "123456789";
	this.dbname = "ecom";
	this.isConnected = false;
	this.getStatus = function()
	{
		return (this.isConnected) ? "Connected to database!" : "Not connected to database";
	};

	//assign the object itself to a public variable instance
	dbconfig5.instance = this;

}
var myDbconfig1 = new dbconfig5();
var myDbconfig2 = new dbconfig5();
myDbconfig1.isConnected=true; //setting the isConnected to true
console.log("myDbconfig1: "+myDbconfig1.getStatus()); //myDbconfig1: Connected to database! 
console.log("Are myDbconfig1 and myDbconfig1 the same db config object? " + (myDbconfig1 === myDbconfig2) ); //Are myDbconfig1 and myDbconfig1 the same db config object? true 
console.log("myDbconfig2: "+myDbconfig2.getStatus()); //myDbconfig2: Connected to database!

6. Yet another Self instantiated function that has a public function getInstance which will always return the same object.

var dbconfig6 = (function()
{
	var instance=this;
	this.host;
	this.user;
	this.password;
	this.dbname;
	this.isConnected;
	this.getStatus = function()
	{
		return (this.isConnected) ? "Connected to database!" : "Not connected to database";
	};

	var init = function()
	{
		host = "localhost";
		user = "ken";
		password = "123456789";
		dbname = "ecom";
		isConnected = false;
	}

	return {
		getInstance:function(){
			if(!instance){
				init();
			}
			return instance;
		}
	}

})();

var myDbconfig1 = dbconfig6.getInstance();
var myDbconfig2 = dbconfig6.getInstance();
console.log("myDbconfig1: "+myDbconfig1.getStatus()); //myDbconfig1: Not connected to database
console.log("Are myDbconfig1 and myDbconfig1 the same db config object? " + (myDbconfig1 === myDbconfig2) ); //Are myDbconfig1 and myDbconfig1 the same db config object? true
console.log("myDbconfig2: "+myDbconfig2.getStatus()); //myDbconfig2: Not connected to database
var temp = new dbconfig6(); //This line will give you the error: Uncaught TypeError: object is not a function

The above 6 are only a few examples of creating a singleton object in javascript for demonstration purposes. There are many other differnt ways to create a singleton object in javascript.

demo , Right click on the mouse, Inspect Element, Click console.

Search within Codexpedia

Custom Search

Search the entire web

Custom Search