Three Different Ways to Create Objects in JavaScript

Object literals are only the beginning

Ashutosh K Singh
Sep 30 · 3 min read
Photo by Markus Spiske on Unsplash

Almost everything in Javascript is an object, whether it’s an array or a function. In this post, we’ll learn three different ways to create objects in JavaScript:

  1. Object Literals
  2. New Keyword
  3. Class

Object Literals

A JavaScript object literal is a comma-separated list of name-value pairs wrapped in curly braces. Object literals encapsulate data, enclosing it in a tidy package.

Object literal property values can be of any data type, including array literals, function literals, and nested object literals.

Shorthand property names

Consider a scenario in which you have to put different variables inside an object. One way of doing it is:

With ECMAScript 2015, a shorter notation is available to achieve the same thing:

Duplicated property names

If you use the same name for two properties, the second property will overwrite the first.

Duplicate Property Names

Keyword

The constructor creates an object wrapper for a given value. If the value is or , it will create and return an empty object. Otherwise, it will return an object of a type that corresponds to the given value.

Objects can also be created using the keyword. With the built-in Object Constructor in Javascript, creates an empty object; or, this keyword can be used with a user-defined constructor function:.

To get started, first take a look at this example:

new Object()

The next step is to add properties and methods to this empty object. This can be achieved with simple dot notation:

However, this practice is not recommended, as there is a scope resolution behind the scenes to check if the constructor function is built-in or user-defined.

User-defined constructor functions

Functions can also be used to create objects in JavaScript. If you really think about it, they’re already objects—so basically, objects are used to create more objects.

Generally, this method is preferred over the object constructor. Imagine you have to create hundreds of objects with the same properties; with the object constructor method you’ll have to manually add all the properties to all the objects but with function constructor, these properties can be predefined.

Using the same function constructor, any number of objects can be created.


Using ES6 Classes to Create Objects

This method is a lot similar to using with the user-defined function constructor. Classes are the primary components of Object Oriented Programming (OOP). Many instances of classes can be created which are in fact objects. The constructor functions can now be replaced by classes, as they are supported in ES6 specifications

Here I have defined all the arguments inside the constructor.

Methods can be a part of the class while declarations can be added later to the created instance of the class, “objects”:

Here this method is a part of the object and will not affect our original class.

Both classes and constructors imitate an object-oriented inheritance model to JavaScript, which is a prototype-based inheritance language.

Being familiar with classes is extremely helpful, as popular JavaScript libraries such as React make frequent use of the syntax.

Better Programming

Advice for programmers.

Ashutosh K Singh

Written by

Writer at Better Programming, Javascript Developer, Works with the Google Assistant, Alexa Skill Developer, UI/UX Designer, find me at https://ashusingh.me/

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade