Java script classes were introduced in ECMAScript 2015. It is also known as ES6. Js class is a blueprint of an object. We use a new keyword when creating a class. And also use constructor() when declaring the class. The important thing is class is not an object. It provides a template for the javascript object.

Example:

Here that example creates a class name called Employee. That class has initial three special properties, those are name, age, and city. The Constructor is a special method. It is used for creating and initializing an object created with a class. When you have a class can create objects using that class.

In the above example, create two objects called e1 and e2 using the Employee Class. Use the ‘new’ keyword when creating a fresh object using the class. When creating a new object constructor method is automatically called.

Constructor and Class Methods

The constructor is a special method for creating and initializing objects created with the class. The constructor is automatically called when a new object created. If did not define a constructor javascript will add an empty constructor. Another thing is if the class contains more than one occurrence of a constructor method it is thrown by a SyntaxError.

Methods are created the same way as object methods. The class can have any number of methods. The method can display using the object.

In that example have Student class. In that class has a method call display. That display method is used to display all the details of Student Object of s1.

Class inheritance

It is the way one class extends to another class. When creating a class inheritance use the extends keyword. A newly created class inherits all the methods and properties of the parent class.

output:

The super() method refers to the parent class. When calling the super method inside the constructor of the child class we can get access to the parent’s properties and methods. In the above example parent class is Employee and that class extends by the Teacher class. When looking at that output we can clearly identify The object of the Teacher class has access to the teacher class method and also the Employee class method.

Getter and Setter

To add getters and setters in the class we use the ‘get’ and ‘set’ keywords. When we want to do something special with value before returning we can use getters and setters.

output:

In that example code get method returns the value of the name. When using the setter we can set the value to that particular property. Here we set a new name for the Student. Above the code output display difference before Student name and after we set the value of the name.

And Many programmers use the underscore before the property to separate the getter/setter from the actual property.

Hoisting

It is the behavior of JavaScript. The simple explanation is we must declare a class before the class use.

output:

Conclusion:

In this article, I briefly explain java classes with the class declaration, constructor, methods, getters and setters, and hoisting.

For more information, you can refer to the below link,

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide