Understanding Classes in JavaScript

When venturing into the vast universe of programming, JavaScript (JS) offers a diverse palette of tools. One of its most powerful and fundamental tools is the "class". But what is a class in JavaScript? Let's dive in.

What is a Class?

Think of a class as a blueprint for creating objects. Objects represent entities in the real world (e.g., cars, students, books). If you consider a "car", it has properties like its color, brand, and speed. It also has actions (methods) like "accelerate" or "brake". In JS, we can define all of these using classes.

In traditional programming, this concept is linked to Object-Oriented Programming (OOP). While JavaScript isn't purely an OOP language, it borrows the concept of classes to provide structure and model real-world entities.

Declaring a Class

In JavaScript, you can declare a class using the class keyword:

class Car {
    constructor(brand, color) {
        this.brand = brand;
        this.color = color;
        this.speed = 0;
    }
}

Here, Car is our class. The constructor is a special method for creating and initializing objects created from the class.

Creating an Object (Instance)

Using our Car blueprint, we can now create a car object:

let myCar = new Car("Toyota", "Red");
console.log(myCar.brand); // Outputs: Toyota
console.log(myCar.color); // Outputs: Red

Methods in Classes

Classes can have functions, termed "methods". Let's give our car the ability to accelerate:

class Car {
    constructor(brand, color) {
        this.brand = brand;
        this.color = color;
        this.speed = 0;
    }

    accelerate(amount) {
        this.speed += amount;
    }
}

Now, we can make our car accelerate:

let myCar = new Car("Toyota", "Red");
myCar.accelerate(20);
console.log(myCar.speed); // Outputs: 20

Getter and Setter

In classes, you can have special methods termed "getters" and "setters" to access or modify object properties:

class Car {
    // ... previous code ...

    get currentSpeed() {
        return this.speed;
    }

    set currentSpeed(value) {
        if(value < 0) {
            this.speed = 0;
        } else {
            this.speed = value;
        }
    }
}

let myCar = new Car("Toyota", "Red");
myCar.currentSpeed = 30;
console.log(myCar.currentSpeed); // Outputs: 30

Inheritance

One of the beauties of classes in JS (and OOP) is inheritance, allowing one class to inherit properties and methods from another. For instance, imagine a SportsCar class that inherits from our Car class:

class SportsCar extends Car {
    accelerate(amount) {
        this.speed += amount * 2; // Sports cars accelerate faster!
    }
}

let mySportsCar = new SportsCar("Ferrari", "Yellow");
mySportsCar.accelerate(20);
console.log(mySportsCar.speed); // Outputs: 40

In the above example, the SportsCar class inherits everything from the Car class but overrides the accelerate method.

Conclusion

JavaScript's class system provides a structured way of representing real-world entities, making it easier to organize, understand, and manage code. As you continue your journey in programming, you'll find classes to be instrumental in many languages and paradigms.

This is a beginner's introduction to classes in JS. As you dive deeper, you'll encounter more advanced topics like static methods, private class fields, and much more. Happy coding!

0
Subscribe to my newsletter

Read articles from saurabh suryavanshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

saurabh suryavanshi
saurabh suryavanshi