Object Constructors in JS


Object constructors are used to create blueprints for an object. They are a more dynamic way of create objects and are very useful when you create many objects of the same type. For example, you can have a Person Object constructor that can be used as a blueprint to create many other objects with name, birthYear and gender properties.

Creating your first Object Constructor (Step 1)

The following creates a person object constructor with properties of name, birthYear ,company and gender.

   
                     //Create a `Person` object constructor
                        function Person(name,birthYear,company,gender){
                            this.name = name;
                            this.birthYear = birthYear;
                            this.company = company;
                            this.gender = gender;
                         }
                       

Using this Object Constructor (Step 2)

After creating the person object constructor, we can use it to create other `people objects`.

   
                     //Create a `Person` object constructor
                        function Person(name,birthYear,company,gender){
                            this.name = name;
                            this.birthYear = birthYear;
                            this.company = company;
                            this.gender = gender;
                         }

                    // Create a new Person object `mark`.
                    const mark = new Person('Mark',1984,'Facebook','male');
                       

Add an object method (Step 3)

Adding a `method` to the existing person object `mark` is done outside the person object constructor

   
                     //Create a `Person` object constructor
                        function Person(name,birthYear,company,gender){
                            this.name = name;
                            this.birthYear = birthYear;
                            this.company = company;
                            this.gender = gender;
                         }

                    // Create a new Person object `mark`.
                    const mark = new Person('Mark',34,'Facebook','male');

                    // Add a method to the existing Person object `mark`
                    mark.greeting = () =>{
                    console.log(`Hi my name is ${mark.name} and I am CEO of Facebook`); // this greeting is unique to mark.
                     }
                       

Use the Object Constructor to create more person objects (Step 4)

Using the Person object constructor we have create above, we can create similar objects using the Person object blueprint.

   
                
                    // Create a new Person object `elon`.
                     const elon = new Person('Elon',1971,'SpaceX','male');

                    // Create a new Person object `bill`.
                     const bill = new Person('Bill',1955,'Microsoft','male');

                      // Create a new Person object `marissa`.
                     const bill = new Person('Marissa',1975,'Yahoo','female');

                  
                       


Dash-Intel is a Power BI and Tableau resource site for data visualization and building BI dashboards.

Data Analyst & Consultant

Copyright 2015-2023 Dash-Intel.com Terms