How to use JavaScript Constructor Function ?

A JavaScript constructor function is a function that is used to create new objects. It is called a constructor because it constructs new objects.

To create a JavaScript constructor function, you use the function keyword. The function name should be descriptive of the type of object that the function will create. For example, a constructor function for a Person object might be called Person.

The body of the constructor function should contain the code that is executed when a new object is created. This code can be used to initialize the properties of the new object.

Here is an example of a JavaScript constructor function that creates a Person object:

<script type="text/javascript">
        function Person(person_name, person_age, person_gender) {
            // assigning  parameter values to the calling object
            this.name = person_name,
                this.age = person_age,
                this.gender = person_gender,
                this.greet = function() {
                    return ('Hi' + ' ' + this.name);
                }
        }
        // creating objects
        const person1 = new Person('Amit', 23, 'male');
        const person2 = new Person('Sumit', 25, 'female');
        // accessing properties
        console.log(person1.name); 
        console.log(person2.name); 
    </script>

Output:-

Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.

Related Posts

How to print Null value Using JavaScript ?

In JavaScript, if you want to print the string “null” when a variable is null, you can do so add in !== operator. Benefits of Using if-else…

How to use Prompt function in JavaScript ?

In this tutorial we’re going to learn how to use Prompt function using JavaScript. Just use below code and put put inside the script tag Output:- I…

JavaScript Comparison and Logical Operators

In this tutorial I’m going to learn how to use Comparison and logical operators in laravel. Comparison operators compare two values and give back a boolean value:…

How to use Let and Const in JavaScript ?

In this tutorial we’re going to learn how to use Const and let function in JavaScript. JavaScript let is used to declare variables. The variables declared using let are block-scoped. This means…

Top 10 IT Certification in The World ?

In this tutorial I’m going to share top 10 IT Certification in the world who help to grow your carrier in fast way also we ‘having covering…

Phantom installation failed TypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be of type string.

Error:- Solutions:- Run this Output :-

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x