Object Creation in JavaScript

 
There is no concept of class in JavaScript till ECMA 5 specification. Concept of classes has been introduced from ECMAScript 6 onwards.
So let's get started about object creation concept. There are three ways to define JavaScript Object. Also before going into the details of it, just want to let you know that there is no concept of class in JavaScript.

1. Using function

This option is very common. We define JavaScript function and then create JavaScript object using 'new'keyword. To define properties and method you can use 'this' keyword.
 
Please see below example of creating object using function.
 
 
function personInfo(firstName, lastName, age){
/*
	Note**: this.firstName and other properties with this keywords 
	are properties of personInfo function/object.
*/
	this.firstName = firstName;
	this.lastName = lastName;
	this.age = age;
	this.fullName = function(){
		return this.firstName + this.lastName;
	};
	
	this.getPersonInfo = getPersonInformation;
	
/*
fullName and getPersonInfo are methods of personInfo.
*/
}

function getPersonInformation(){
	return this.firstName + " " +this.lastName + " with age " + this.age;
}
 
You can instantiate the personInfo object using personInfo constructor function, also you can add properties and do a method call on the object. Following thing can be done with an object.
 
 
var person = new PersonInfo("Deepak", "Singh", "35");
console.log(person.fullName()); // Deepak Singh
console.log(person.getPersonInfo()); // Deepak Singh with age 35.
person.firstName= "Robert";
	 

Problem with the above example is that it will recreate getPersonInfo() or fullName() every time we create an object of personInfo. But We can fix this issue or add methods to prototype of constructor function in inexpensive way. Please see the example below.

 

function personInfo(firstName, lastName, age){

	this.firstName = firstName;
	this.lastName = lastName;
	this.age = age;
	this.fullName = function(){
		return this.firstName + this.lastName;
	};
	
	this.getPersonInfo = getPersonInformation;
	
}
personInfo.prototype.fullName = function(){
		return this.firstName + this.lastName;
};

personInfo.prototype.getPersonInformation = function(){
		return this.firstName + " " +this.lastName + " with age " + this.age;
};

2. Using Object literals

One of the simplest and shorter way of defining javascript object and array using object literals
You can create empty object simply.
 
 
 var obj = {};
 or 
 var obj = new Object();
 Array declaration:
 var arr = [];
 or
 var arr = new Array();

Here is the Simple example:

 

personInfo = {

	this.firstName = "Deepak";
	this.lastName = "Singh";
	this.age = 32;
	this.fullName = function(){
		return this.firstName + this.lastName;
	};
	
	this.getPersonInfo = getPersonInformation;

}

function getPersonInformation(){
	return this.firstName + " " +this.lastName + " with age " + this.age;
}
 
The above example of object creation is an example of Singleton pattern. With this pattern of object creation you cannot instantiate this object,because it is already exsts/created.
 

3. Singelton Using function

 
Another way of creating singleton object is using function. Here is the example.
 
 
personInfo = new function(){

	this.firstName = "Deepak";
	this.lastName = "Singh";
	this.age = 32;
	this.fullName = function(){
		return this.firstName + this.lastName;
	};
	
	this.getPersonInfo = getPersonInformation;

}

function getPersonInformation(){
	return this.firstName + " " +this.lastName + " with age " + this.age;
}
 
So that's all bout object creation. Hope this would be useful article and given you understanding of object creation in JavaScript.
 
Keep Reading and Keep learning.