Creating JavaScript Objects

Introduction

 
In my previous article, I explained what a JavaScript Object is. Now let us move further to the creation of objects.
 
We can create objects in the following two ways:
  1. Object Literals
  2. Object Constructor

Object Literals

 
The most common and easiest way to create objects is with the object literal described here:
  1. // This is an empty object initialized using the object literal notation  
  2. var employee = {};  
  3. // This is an object, again using object literal  
  4. var employee = {  
  5. id:20984  
  6. name: "Roger",  
  7. department: "IT",  
  8. getMessage: function () {  
  9. console.log("New Message");  
  10. }  
  11. }  

Object Constructor

 
The second most common way to create objects is with an Object constructor. A constructor is a function used for initializing new objects and you use the new keyword to call the constructor.
  1. var employee = new Object();  
  2.     employee.id = 20984;  
  3.     employee.name = "Roger"; employee.department = "IT";  
  4.     employee.getDetails = function () {  
  5.     console.log("This is a " + this.name + "working in " + this.department);  
  6. }  
Objects can contain any other data type, including numbers, arrays, and even other objects.
 

Practical Patterns for Creating Objects

 
For simple objects that may only ever be used once in your application to store data, the above two methods can be used for creating objects
 
Imagine you have an application that displays Employees and the details of each employee. All employees in your application have the following properties: id, name, designation, department, preferredLocation, getDetails, and getpreferredLocations functions.
 
It would be quite tedious and counter-productive to type the following every time you want to create a new employee object.
  1. var employee = {  
  2. id: 20984,  
  3. name: "Roger",  
  4. designation: "Project Manager",  
  5. department: "IT",  
  6. preferredLocation: ["Mumbai""Pune"],  
  7. getDetails : function () {  
  8.    console.log("This is a " + this.name + "working as " + this.designation);  
  9. }  
  10. getpreferredLocations: function () {  
  11. this.preferredLocation.forEach(function (eachCity) {  
  12. console.log("Works at:" + eachCity);  
  13. });  
  14. }  
  15. }  
  16. }  
If you have 10 employees then you will need to add the same code 10 times. And what if you need to make a change to the getpreferredLocations function? You will need to make the change in 10 places.
 
To solve these repetitive problems, software engineers have invented patterns (solutions for repetitive and common tasks) to make developing applications more efficient and streamlined.
 

Constructor Pattern for Creating Objects

  1. function employee (emp_id, emp_name, emp_designation, emp_department, emp_preferredLocation) {  
  2.         this.id = emp_id;  
  3.         this.name = emp_name;  
  4.         this.designation = emp_designation;  
  5.         this.department = emp_department;  
  6.         this.preferredLocation = emp_preferredLocation;  
  7.         this.getDetails = function () {  
  8.             console.log("This is a " + this.name + "working as " + this.designation);  
  9.         }  
  10.         this.getpreferredLocations = function () {  
  11.             this.preferredLocation.forEach(function (eachCity) {  
  12.                 console.log("Works at:" + eachCity);  
  13.             });  
  14.         }  
  15.     }  
With this pattern in place, it is very easy to create all Employees:
  1. var softwareEngineerEmp = new employee(20989, "Dominic""Software Engineer""IT", ["Banglore""Chennai"]);  
  2. softwareEngineerEmp.getDetails(); // This is a Dominic working as Software Engineer.  
  3. softwareEngineerEmp.getpreferredLocations();  
  4. // Works at:Banglore  
  5. // Works at:Chennai   
  6. var AccountantEmp = new employee(19899, "Sam""Accountant""Account", ["Mumbai"]);  
  7. AccountantEmp.getDetails(); // This is a Sam working as Accountant.  
  8. AccountantEmp.getpreferredLocations(); // Works at: Mumbai 
If you need to change the getDetails function then you only need to do it in one location. The pattern encapsulates all the functionalities and characteristics of the entire employee by making just the single employee function with inheritance.
 

Prototype Pattern for Creating Objects

  1. function Employee () {  
  2. }  
  3. Employee.prototype.id = 20999;  
  4. Employee.prototype.name = "Kevin";  
  5. Employee.prototype.designation = "Delivery Head";  
  6. Employee.prototype.department = "IT";  
  7. Employee.prototype.preferredLocation = ["Banglore""USA"];  
  8. Employee.prototype.getDetails = function () {  
  9. console.log("This is a " + this.name + "working as " + this.designation);  
  10. }   
  11. Employee.prototype.getpreferredLocations = function () {  
  12.     this.preferredLocation.forEach(function (eachCity)  {  
  13.    console.log("Works at:" + eachCity);  
  14.     );  
  15. }     
And this is how we call the Employee() constructor in this prototype pattern:
  1. var employee = new Employee();  
  2. employee.getDetails(); //  This is a Kevin working as Delivery Head.  
  3. employee.getpreferredLocations();  
  4. //  Works at:Banglore  
  5. //  Works at:USA 
I have tried to cover the most basic features that will help to start with creating JavaScript objects. Thanks for reading. Please provide your inputs and suggestions for the article.


Similar Articles