Home » JavaScript

Different ways to create an Object in JavaScript

An object is a group of data that is stored as a series of name-value pairs encapsulated in one entity. In this article, we will learn different ways to create an object in JavaScript.
Submitted by Abhishek Pathak, on October 31, 2017

Objects are the core of the JavaScript. Everything in JavaScript is based on the objects. Strings, Arrays and even functions are objects in JavaScript. JavaScript is the object based language and follows some OOPs features. In this article we will learn different ways to create an object in JavaScript.

An object is a group of data that is stored as a series of name-value pairs encapsulated in one entity. Each item in the list is called a property and the functions are called methods. But to use the objects, we have to create them and in JavaScript, we get different types of ways to do it.

1. Using Object Literal

The most common way of Object declaration is using the Object literal. It is directly creating the object in a variable by defining properties and methods to it. It is the recommended method as it is brief and works very well. Here is an example.

Code

var car = {
	type: "sedan",
	doors: 4,
	enterCar: function() {	
			console.log("Inside Car"); 
		  }
}

2. Using Object Constructor

Using the Object constructor function Object() we can create an object using new keyword. The Object definition can be passed as an argument to object constructor. Here's an example.

Code

var car = new Object({
	type: "sedan",
	doors: 4,
	enterCar: function() {	
			console.log("Inside Car"); 
		  }
});

The Object constructor works in similar way, expect that everything is passed as a parameter.

3. Using function constructor

If you want to create custom objects than this is the method you require. Here you define the properties of the object inside the function and then create a new object based on it. The example will make more sense.

Code

var car = function(t, d) {
	this.type = t;
	this.doors = d;
	this.enterCar = function() {
		console.log("Inside Car"); 
	}
};

//Creating an object of this function
var audi = new car('sedan', 2);

These were the methods that are used to create and define new objects. You'll find mostly the first method to create objects in the most of the JavaScript programs. Hope you like the article. Share your thoughts in the comments below.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.