JavaScript Lessons

What is constructor in javascript class

Hello, today will see, how constructors in JavaScript classes works A constructor is a method that starts automatically when you create a new instance of a any class. Constructor is used for initialization instance of class and is used too for creating properties of this instance. If you dont write constructor into class, such class has defualt constructor, which is empty.

We will show the advantages of writing and using own constructor. We have class Student, you can see, this class is empty and dont contain any visible constructor. It means, that here is only empty default constructor. Now we create instance of class Student, with name student1. If we want add any properties for this instance, for example first name and last name of this student1, we must use access to this instance via dot. So we can write student1.firstname equals John and we can also write student1.lastname equals black. Now is possible writte both values into document and see, that properties firstname and lastname were added to instance student1 corectly. In other words, we can say that instance student1 now contains properties firstname and lastname, to which specific values ​​are assigned. And this values are value John in property firstname and value Black in property lastname.

Better and more comfortable way how to work with properties of any instance, is writting own constructor. So now we comment class Student and uncomment class Teacher. We can see, that in class Teacher is constructor with two parameters, firstName and lastName. In the body of the constructor, we create properties firstname and lastname, and these properties will be equal to the arguments that we write in the constructor parameter when creating a new instance of Class Teacher. It is important to mention here that we write properties in the constructor after the keyword this. The this keyword has several uses in JavaScript. In this case, the keyword this generally means an instance of the class. Each instance of the class we create will have properties, which we create after the this keyword. Joining of the keyword this and property is by using dot notation.

In the next step we create a new instance od class Teacher, name of this instance will be teacher1. We have a constructor with two parameters defined for the Teacher class, so we cen give arguments for instance teacher1. The argument for the firstName parameter will be Joe and the argument for the lastName parameter will be Cooper. We will now write both property values ​​of the teacher1 instance to the document. Now try to launch the page and see that the first name and last name of the teacher1 instance will be displayed correctly on the page.

The complete source code as well as the complete text tutorial for this video is located in the link written in the video description. Thank you for watching this video and if you like, subscribe to this channel and put like.

Download source files