Javascript Object Oriented Programming

Javascript is very simple to use in term of object oriented programming, compaired to other "pure" object oriented languages such as c++, Java, C# etc. Javascript has a number of built in objects that allows to create new objects and properties. User can create new object by creating instance of the object.

Javascript object is a simple collection of properties, properties nothing but key-value pair. Here value can be any primitive type, an object, or a function. Javascript properties can be added, updated and deleted at any point of time. The general format of creating an object in Javascript using an object initializer are

objectName = {property1:value1, property2:value2, property3:value3, property4:value4...}

Where "objectName" is the name of object and propertyX:valueX is the name value pair. There are different way to create object in Javascript:

Using Object Constructor

This is simple way to create an object using Object constructor.

Javascript Using Object Constructor Example

var person = new Object();  
person.setPerson = function(name,address,email){  
    this.name = name;  
	this.address = address;  
	this.email = email;  
}; 

//To create person object use the following statement
person.displayPerson = function(){  
    document.write (this.name);  
	document.write (this.address);  
	document.write (this.email);   
}; 

//Setting Person object properties
person.setPerson('Peter', '12/A12','peter@xyz.com');

//Display person object properties
person.displayPerson();

This will produce following result

Peter,12/A12,peter@xyz.com

Using Literal Notation

The other way of defining an object is via literal notation.

Javascript Using Literal Notation Example

var person = {
	name : 'Peter',  
	address : '12/A12',  
	email : 'peter@xyz.com',
	setPerson: function(name,address,email){  
		this.name = name;  
		this.address = address;  
		this.email = email;   
	},
	displayPerson: function(){  
		document.write (this.name);  
		document.write (this.address);  
		document.write (this.email);   
	}
}  
//Display person object properties
person.displayPerson();
//Set person object properties
person.setPerson('Jess','12-13-284','Jess@yahoo.com');
//Display person object properties
person.displayPerson();

This will produce following result

Jess,12-13-284,Jess@yahoo.com

Using Factory Function

The other way Factory function allows to encapsulate the logic for creating similar objects.

Javascript Using Factory Function Example

var objPerson = function(name,address,email){  
     var result = new Object(); 
        result.name = name;  
		result.address = address; 
		result.email = email; 
        result.displayPerson= function(){  
            document.write(this.name);  
            document.write(this.address);  
            document.write(this.email);  
        };
return result;
};  
var personOne = objPerson ('Tom','232-323','tom@yahoo.com');  
personOne.displayPerson();

This will produce following result

Tom,232-323,tom@yahoo.com

Using Object Constructor

In Javascript functions are objects, you can call any function using the new operator in front of it. It would be nice if all instances of Person share the same displayPerson object, since this holds behavior and not data.

Javascript Using Object Constructor Example

function Person(name,address,email){  
        this.name = name;  
		this.address = address; 
		this.email = email; 
        this.displayPerson= function(){  
            document.write(this.name);  
            document.write(this.address);  
            document.write(this.email);  
        }
};  
var personOne = new Person('Tom','232-323','tom@yahoo.com');  
personOne.displayPerson();

This will produce following result

Tom,232-323,tom@yahoo.com

Using Prototype

In Javascript functions are objects, they can create other objects and they automatically get a field called prototype. Every object created through a function inherits the function's prototype.

Javascript Using Prototype Example

function Person(){};  
Person.prototype.name = "Jimi"; 
//Creating two objects
var objOne = new Person();  
var objTwo = new Person(); 
 
document.write(objOne.constructor == Person); // prints true  
document.write(objOne.name); // This will prints Jimi  
document.write(objTwo.constructor == Person); // prints true  
document.write(objTwo.name); // This will print Jimi 

This will produce following result

true, Jimi, true, Jimi

Using Combination of Function/Prototype

The function/prototype combination takes advantage of both approaches.

Javascript Using Combination of Function/Prototype Example

function Person(name,address,email){  
        this.name = name;  
		this.address = address; 
		this.email = email;  
};  
Person.prototype.displayPerson = function(){  
            document.write(this.name);  
            document.write(this.address);  
            document.write(this.email);  
        };
var personOne = new Person("Jimi","12-13-283","jimi@yahoo.com");  
var personTwo = new Person("Tom","A12/283","tom@gmail.com"); 
personOne.displayPerson();   
personTwo.displayPerson();   
document.write(personOne.displayPerson === personTwo.displayPerson)

This will produce following result:

Jimi, 12-13-283, jimi@yahoo.com
Tom, A12/283, tom@gmail.com
true

Using Singleton

Sometimes, you may need to make sure that only a single instance of a certain object exists. In Javascript you can simply define and invoking the constructor at the same time.

Javascript Using Combination of Function/Prototype Example

var singleton = new function(){  
    this.webSiteName = "www.techstrikers.com";  
};
document.write(singleton.name);

This will produce following result:

www.techstrikers.com