JavaScript – Constructor Method


What is a constructor method ?

This is a method used to create and initialise a javascript object.

Whenever the memory is allocated for javascript object constructor method is called.

How to create constructor method ?

“constructor” keyword is used to declare a constructor method.

Javascript class can contain only one constructor method.


Parent class constructor can be accessed by using “super” keyword.

Basics of javascript for beginners !!!

Click to Learn More about – Javascript online learning

Syntax :

constructor(argument1,argument2,argument3...argumentN);

1. Simple Constructor inside a class

Example :

 <script>
                        class StudentInfo{
                                constructor(name,age,address)
                                {
                                        this.name = name;
                                        this.age = age;
                                        this.address = address;
                                }
                        }
                        var a = new StudentInfo("Abdul","33","Chennai");
                        document.writeln("Name = " + a.name+"<br/>");
                        document.writeln("Age = " + a.age+"<br/>");
                        document.writeln("Address = " + a.address+"<br/>");
                </script>

Output :

2. Constructor with “super” keyword.

Suppose we have a main class “StudentInfo” and sub class “StudentMarksInfo”.

Example :

  <script>
                        class StudentInfo{
                                constructor()
                                {
                                        this.name = "Abdul";
                                        this.age = "14";
                                        this.address = "Chennai";
                                }
                        }

                        class StudentMarksInfo extends StudentInfo{
                                constructor(english,tamil,maths,science,biology,history,geography){
                                        super();
                                        this.english = english;
                                        this.tamil = tamil;
                                        this.maths = maths;
                                        this.science = science;
                                        this.biology = biology;
                                        this.history = history;
                                        this.geography = geography;
                                }
                        }
                        var b = new StudentMarksInfo("87","99","100","98","87","100","77");
                        document.writeln("Name = " + b.name+"<br/>");
                        document.writeln("Age = " + b.age+"<br/>");
                        document.writeln("Address = " + b.address+"<br/>");
                        document.writeln("English Mark = " + b.english+"<br/>");
                        document.writeln("Tamil Mark = " + b.tamil+"<br/>");
                        document.writeln("Maths Mark = " + b.maths+"<br/>");
                        document.writeln("Science Mark = " + b.science+"<br/>");
                        document.writeln("Biology Mark = " + b.biology+"<br/>");
                        document.writeln("History Mark = " + b.history+"<br/>");
                        document.writeln("Geography Mark = " + b.geography+"<br/>");
                </script>

Output :