JavaScript, the widely-used web scripting language, gained class-like object orientation with ECMAScript 2015. This feature has been a significant addition, allowing developers to define blueprints for creating objects, and implement inheritance and encapsulation. Let's dive into an example to understand JavaScript Classes better.

Before we proceed with the example, ensure you have a basic understanding of object-oriented programming concepts such as classes, inheritance, and methods. If not, a brief refresher on these might be beneficial before diving into the code.

Defining a JavaScript Class: Employee Example
Let's define a simple 'Employee' class to demonstrate JavaScript classes. This class will have properties like 'firstName', 'lastName', and 'role', along with methods for greeting and describing the employee.

Here is a basic structure of the 'Employee' class. We'll break down each section and explain it in detail.
Class Definition & Properties

The 'class' keyword is used to define a new class, followed by the class name. Inside the class, we use the 'constructor' method to initialize properties. These properties become the object's initial state. In our 'Employee' class, we have 'firstName', 'lastName', and 'role'.
Here's the code:
```javascript class Employee { constructor(firstName, lastName, role) { this.firstName = firstName; this.lastName = lastName; this.role = role; } } ```
Adding Methods

Methods in JavaScript classes are defined just like in regular objects, using the 'this' keyword to refer to object properties. In our 'Employee' class, we add two methods: 'greet', which prints a greeting message, and 'describe', which logs the employee details.
Here's how we add these methods:
```javascript class Employee { constructor(firstName, lastName, role) { this.firstName = firstName; this.lastName = lastName; this.role = role; } greet() { console.log(`Hello, ${this.firstName} ${this.lastName}!`); } describe() { console.log(`${this.firstName} ${this.lastName} is a ${this.role}`); } } ```
Creating Instances & Using Methods

Once the class is defined, we can create new instances (objects) of the 'Employee' class. Let's create an 'Engineer' and use the added methods.
Here's how to create an 'Engineer' and call the 'greet' and 'describe' methods:




















```javascript const engineer = new Employee('John', 'Doe', 'Engineer'); engineer.greet(); // Output: Hello, John Doe! engineer.describe(); // Output: John Doe is a Engineer ```
Inheritance with JavaScript Classes
One of the powerful features of JavaScript classes is inheritance. Let's create a 'Manager' class that inherits from the 'Employee' class.
Here's the 'Manager' class with the 'addEmployee' method:
```javascript class Manager extends Employee { constructor(firstName, lastName, role, employees) { super(firstName, lastName, role); this.employees = employees; } addEmployee(employee) { this.employees.push(employee); } } ```
Using Inheritance
With inheritance, the 'Manager' instance has access to all methods and properties from the 'Employee' class. Plus, it adds a new 'addEmployee' method.
Let's create a 'Manager' instance and use its methods:
```javascript const manager = new Manager('Jane', 'Smith', 'Manager', []); manager.greet(); // Output: Hello, Jane Smith! manager.describe(); // Output: Jane Smith is a Manager manager.addEmployee(engineer); // Adds 'engineer' to the 'employees' list. ```
This example demonstrates how to define and use JavaScript classes, including inheritance. By having a good understanding of JavaScript classes, you can create more organized, reusable, and maintainable code. Happy coding!