Creating Javascript Objects in Different ways

Objects are entities that have properties and functions to mimic real-world objects. In Javascript, objects are non-primitive data types. The property of an object refers to the fact that it can hold primitive data types.

There are several ways to create javascript objects. We will discuss a few of them.

  • using javascript class
  • using constructor function
  • factory function

 

Creating Object using Javascript class

Classes are nothing but a template for creating objects. It performs encapsulation and wraps data and functions into a single entity. In javascript, classes are declared using the keyword ‘class’. Classes are one of the ways of creating objects in javascript.

In the below example, we declared a class called CreatingObject which has a constructor, a data member, and a method called getMessage(). We create the object using the ‘new’ keyword followed by the class name. Then using the object, we call the method getMessage().

class CreatingObject {
    constructor(message) {
        this.message = message
    }
    getMessage() {
        console.log(this.message)
    }
}

obj = new CreatingObject('Creating objects using classes')
obj.getMessage()
Creating objects using classes

 

Creating Object using Javascript constructor

The other way of creating objects is using constructors. In the below example, we are creating a function called creatingObject() which takes an argument. It has a data member and a function called getMessage(). We then create the object using the ‘new’ keyword. Then using the object, we call the getMessage() method.

function creatingObject(message) {
    this.message = message
    this.getMessage = () => console.log(this.message)
}

obj = new creatingObject('Creating object using Constructor')
obj.getMessage()
Creating object using Constructor

 

Creating Objects using Javascript Factory Functions

Javascript Factory functions are simply functions that return an object without using the keyword ‘new’. So the result of calling such functions is the return of the object. Whereas a constructor/class uses the keyword ‘new’ to return an object.

Benefits of  using Factory functions

  • So the first benefit is that its definition itself, that is, no need to use the ‘new’ keyword to create an object.
  • They are more flexible compared to constructors/classes.
  • It makes object creation easier and is particularly useful for functions from native code in a browser.

These are the few benefits of factory functions.

In the below example, factoryFunction() is a simple factory function. It takes a number as an argument and returns an object. The object is nothing but a method that returns the square of that number. Therefore, to declare this object, the keyword ‘new’ is not necessary. Just calling the function creates the object.

function factoryFunction(number) {
    return {
        square() {
            return number * number
        }
    }
}

object = factoryFunction(14)
console.log(object.square())
196

 

Please post your comments, queries, doubts in the comment box. Happy Learning 🙂

Also, read

Leave a Reply

Your email address will not be published. Required fields are marked *