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.

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.

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.

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.

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.

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.

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.

var singleton = new function(){  
    this.webSiteName = "localhost/tslocal";  
};
document.write(singleton.name);

This will produce following result:

localhost/tslocal

See Also

Javascript Built in String Object
Javascript Built in Number Object
Javascript Built in Math Object
Javascript Built in Date Object
Javascript Built in Arrays Object
Javascript Built in Boolean Object
Javascript Built in RegExp Object
Javascript Built in JSON Object


 
SHARE THIS IF YOU LIKE!
submit to reddit
SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.