Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Object Constructor

JS Object Constructor

The Object() constructor turns the input into an object. Its behavior depends on the input's type.

					 
        
          function User(name, email) {
            this.name = name;
            this.email = email;
          }

          const employee = new User("John Doe", "john@gmail.com");
          alert(employee.name); // "John Doe"
          alert(employee.email); // "john@gmail.com"
        
      

It is considered good practice to name constructor functions with an upper-case first letter.

In a constructor function this does not have a value. It is a substitute for the new object. The value of this will become the new object when a new object is created.

Object Types (Blueprints) (Classes)

Objects created by object literal or new Object() are limited and create only single object.

Sometimes we need a "blueprint" for creating many objects of the same "type".

The way to create an "object type", is to use an object constructor function. This way we can create multiple objects of the same type.

					 
        
          function Employee(name, email) {
            this.name = name;
            this.email = email;
          }

          const manager = new Employee("John Doe", "john@gmail.com");
          const receptionist = new Employee("Neelam Singh", "neelam@gmail.com");
        
      
Adding a Property to an Object

Adding a new property to an existing object is easy. The following example add the age property to manager.

Note that the property will be added to manager only, not to receptionist. (Not to any other Employee objects).

					 
        
          manager.age = 18;
        
      
Adding a Method to an Object

Adding a new method to an existing object is easy. The following example add the greeting method to manager.

Note that the method will be added to manager only, not to receptionist. (Not to any other Employee objects).

					 
        
          manager.greeting = function () {
            return "Hello " + this.name;
          };
        
      
Adding a Property to a Constructor

You cannot add a new property to an object constructor the same way you add a new property to an existing object.

To add a new property to a constructor, you must add it to the constructor function. This way object properties can have default values.

					 
        
          Employee.age = 18;

          // or
          function Employee(name, email) {
            this.name = name;
            this.email = email;
            this.age = 18;
          }
        
      
Adding a Method to a Constructor

Your constructor function can also define methods. You cannot add a new method to an object constructor the same way you add a new method to an existing object.

Adding methods to an object constructor must be done inside the constructor function.

					 
        
          function Employee(name, email) {
            this.name = name;
            this.email = email;
            this.greeting = function () {
              return "Hello " + this.name;
            };
          }
        
      
Built-in JavaScript Constructors

JavaScript has built-in constructors for native objects.

					 
        
          new String();   // A new String object
          new Number();   // A new Number object
          new Boolean();  // A new Boolean object
          new Object();   // A new Object object
          new Array();    // A new Array object
          new RegExp();   // A new RegExp object
          new Function(); // A new Function object
          new Date();     // A new Date object
        
      
Best Practice

As you can see above, JavaScript has object versions of the primitive data types String, Number, and Boolean. But there is no reason to create complex objects. Primitive values are much faster.

  • Use string literals "" instead of new String()
  • Use number literals 50 instead of new Number()
  • Use boolean literals true / false instead of new Boolean()
  • Use object literals {} instead of new Object()
  • Use array literals [] instead of new Array()
  • Use pattern literals /()/ instead of new RegExp()
  • Use function expressions () {} instead of new Function()
					 
        
          let x1 = "";             // new primitive string
          let x2 = 0;              // new primitive number
          let x3 = false;          // new primitive boolean
          const x4 = {};           // new Object object
          const x5 = [];           // new Array object
          const x6 = /()/          // new RegExp object
          const x7 = function(){}; // new function
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved