codequick-darkmode-logo
लॉगिनसाइन अप
  • javascript-advance

Understanding Classes in ES6

In ES6 (ECMAScript 2015), a new feature called classes was introduced to JavaScript. Classes provide a more intuitive and convenient way to define objects and work with inheritance. They are a syntactical sugar over JavaScript's existing prototype-based inheritance model. This article will introduce you to the concept of classes in ES6 and show you how to use them effectively in your JavaScript code.

What are Classes in ES6?

A class in ES6 is essentially a blueprint for creating objects. It encapsulates data and functions into a single logical unit, making it easier to manage and organize your code. Classes in ES6 follow a similar syntax to classes in other object-oriented programming languages such as Java or C++. With classes, you can define properties and methods specific to a class, create instances of the class, and perform operations on those instances.

Here's an example of a basic class definition in ES6:

class Rectangle { constructor(width, height) { this.width = width; this.height = height; } get area() { return this.width * this.height; } set area(value) { throw new Error('Cannot set the area directly. Use width and height properties instead.'); } toString() { return `Rectangle (width: ${this.width}, height: ${this.height})`; } }

In the example above, we define a class called Rectangle and specify its properties (width and height) in the constructor. We also define a getter and a setter for the area property, which calculates the area of the rectangle. Finally, we have a toString method that returns a string representation of the object.

Creating Instances of a Class

Once you have defined a class, you can create instances of it using the new keyword. Here's an example:

const rectangle = new Rectangle(5, 10); console.log(rectangle.area); // Output: 50 console.log(rectangle.toString()); // Output: "Rectangle (width: 5, height: 10)"

In the example above, we create a new instance of the Rectangle class and pass in the width and height as arguments to the constructor. We can then access the area property and call the toString method on the created object.

Inheritance with Classes

Classes in ES6 also support inheritance, allowing you to create subclasses that inherit properties and methods from a parent class. To define a subclass, you use the extends keyword followed by the name of the parent class. Here's an example:

class Square extends Rectangle { constructor(side) { super(side, side); } toString() { return `Square (side: ${this.width})`; } }

In the example above, we define a subclass called Square that extends the Rectangle class. It has its own constructor that accepts a single argument for the side length. We use the super keyword to invoke the constructor of the parent class and pass in the side length twice to create a square.

Now, let's create an instance of the Square class:

const square = new Square(5); console.log(square.area); // Output: 25 console.log(square.toString()); // Output: "Square (side: 5)"

In the example above, we create a new instance of the Square class and pass in the side length as an argument. We can then access the area property and call the toString method, which is overridden in the Square class.

Conclusion

The introduction of classes in ES6 brings a more familiar and structured approach to object-oriented programming in JavaScript. With classes, you can create reusable and maintainable code by defining properties and methods within a class, and easily create instances and work with inheritance. Classes simplify the process of building complex JavaScript applications and make your code more organized and easier to understand.

If you want to explore more about classes in ES6, check out the MDN documentation on classes or the W3Schools guide on JavaScript classes.