Creating objects in JavaScript (4 Different Ways)

JavaScript is a flexible object-oriented language when it comes to syntax. In this article, we will see the different ways to instantiate objects in JavaScript.
Before we proceed it is important to note that JavaScript is an object-based language based on prototypes, rather than being class-based. Because of this different basis, it can be less apparent how JavaScript allows you to create hierarchies of objects and to have an inheritance of properties and their values.

Creating object with a constructor:

One of the easiest way to instantiate an object in JavaScript. Constructor is nothing but a function and with help of new keyword, constructor function allows to create multiple objects of same flavor as shown below:

filter_none

edit
close

play_arrow

link
brightness_4
code

//simple function
function vehicle(name,maker,engine){
    this.name = name;
    this.maker = maker;
    this.engine = engine;
}
//new keyword to create an object
let car  = new vehicle('GT','BMW','1998cc');
//property accessors
console.log(car.name);
console.log(car.maker);
console.log(car['engine']);

chevron_right


Output:

Explanation: A class in OOPs have two major components, certain parameters and few member functions. In this method we declare a function similar to a class, there are three parameters, name, maker and engine ( the this keyword is used to differentiate the name,maker,engine of the class to the name,maker,engine of the arguments that are being supplied.). We then simple create an object obj of the vehicle, initialize it and call it’s method.

 



Using object literals:

Literals are smaller and simpler ways to define objects.We simple define the property and values inside curly braces as shown below:

filter_none

edit
close

play_arrow

link
brightness_4
code

//creating js objects with object literal
let car = {
    name : 'GT',
    maker : 'BMW',
    engine : '1998cc'
};
//property accessor
console.log(car.name); //dot notation
console.log(car['maker']); //bracket notation

chevron_right


Output:

In the above code we created a simple object named car with the help of object literal,having properties like name,maker,engine.Then we make use of the property accessor methods(Dot notation,Bracket notation) to console.log the values.
Now let’s see how we can add more properties to an already defined object:

filter_none

edit
close

play_arrow

link
brightness_4
code

let car = {
    name : 'GT',
    maker : 'BMW',
    engine : '1998cc'
};
//adding property to the object
car.brakesType = 'All Disc';
console.log(car);

chevron_right


We added new property called brakesType to the above defined car object and when we console.log the entire object we get:
Output:

Methods can also be part of object while creation or can be added later like properties as shown below:

filter_none

edit
close

play_arrow

link
brightness_4
code

//adding methods to the car object
let car = {
    name : 'GT',
    maker : 'BMW',
    engine : '1998cc',
    start : function(){
        console.log('Starting the engine...');
    }
};
car.start();
// Adding method stop() later to the object
car.stop = function() {
    console.log('Applying Brake...');  
}
car.stop();

chevron_right


Output:

Explanation:In the above code start method was added to the car object and later called by the car.start() and also the stop method was added too after the object was already declared.

 

Creating object with Object.create() method:

The Object.create() method creates a new object, using an existing object as the prototype of the newly created object.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

const coder = {
    isStudying : false,
    printIntroduction : function(){
        console.log(`My name is ${this.name}. Am I studying?: ${this.isStudying}`);
    }
};
const me = Object.create(coder);
me.name = 'Mukul';
me.isStudying = true;
me.printIntroduction();

chevron_right


Output:

Using es6 classes:

ES6 supports class concept like any other Statically typed or object oriented language. So, object can be created out of a class in javascript as well as shown below:

filter_none

edit
close

play_arrow

link
brightness_4
code

//using es6 classes
class Vehicle {
  constructor(name, maker, engine) {
    this.name = name;
    this.maker =  maker;
    this.engine = engine;
  }
}
  
let car1 = new Vehicle('GT', 'BMW', '1998cc');
  
console.log(car1.name);  //GT

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : immukul