What Are JavaScript Classes?

Classes in ES6 are like any other programming language and this adds a lot of object oriented properties to JS. Class syntax is very simple and readable. 

class Vehicle {
    constructor(wheels, maxSpeed){
        this.wheels = wheels;
        this.maxSpeed = maxSpeed;
    }
    
    getWheels(){
        return this.wheels
    }
}

let triCycle = new Vehicle(3, 20);

console.log(triCycle.wheels)
console.log(triCycle.maxSpeed)
console.log(triCycle.getWheels())

Now this keyword here is very important. It actually creates an Object inside the class to access its properties and functions. Whenever we call this inside the class, it means we are referring to properties and functions of the class from inside. 

Another keyword, new is used to create a new object of the class. The constructor method is very important. It initialises the object. We can leave it empty but if we want to add few properties with the object on its initialisation then we need to use the constructor method. 

Now ES6 classes can be extendable as well.

class Vehicle {
    constructor(wheels, maxSpeed){
        this.wheels = wheels;
        this.maxSpeed = maxSpeed;
    }
    
}

class Car extends Vehicle {
    constructor(wheels = 4, maxSpeed = 200, color){
        super(wheels, maxSpeed);
        this.color = color;
    }
}


let newCar = new Car(4, 180, 'black');

console.log(newCar.wheels)
console.log(newCar.maxSpeed)
console.log(newCar.color)

The keyword super is just passing values to the parent class constructor here.Â