bookmark_borderWhat is a class in JavaScript

JavaScript Class are the templates for creating the Object which helps the developer to reuse it. JavaScript Class is introduced in the ES6 version.

Keyword class is used to create JavaScript Class and constructor is a special method it will automatically execute when a new object is created.

It is used to initialize object properties, if the constructor does not define in the class it will return empty.

Let’s create a class with and without a constructor then we will inherit the classes.

class a {
 // no constructor

// create a object from `a` class
objA = new a;

// return empty result
class b {
	// with construtor
	constructor() {
		document.write('welcome to b class');

// create a object from `b` class
objB = new b;

// return result from constructor
// we have just print the value inside the constructor not even called it.
// when we create an object it will automatically call the constructor.

Let’s inherit two class with methods using extends keyword

class aa {

	constructor() {
		document.write('welcome to a class')

class bb extends aa {
	constructor() {
                // super keyword used to access parent constructor 
		document.write('welcome to b class')

       test() {
           document.write('<br>this is test method')

objBB = new bb;

The above code will output the result :

welcome to a class
welcome to b class
this is test method

super keyword will used to display the parent class in child class, if we don’t use super keyword in child class it will throw the error message.

Must call super constructor in derived class before accessing ‘this’ or returning from derived constructor.

Please share your suggestion.