Mastering Javascript Classes Example A Complete Guide

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.

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

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.

The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had

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.

Dynamic Class Toggle in DOM
Dynamic Class Toggle in DOM

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

Class Definition & Properties

JavaScript Basics You MUST Know
JavaScript Basics You MUST Know

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

JavaScript this Keyword: Master Context Like a Pro 🚀
JavaScript this Keyword: Master Context Like a Pro 🚀

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

The Complete Guide to JavaScript Classes
The Complete Guide to JavaScript Classes

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:

How to Use classList in JavaScript
How to Use classList in JavaScript
JavaScript Basics for Beginners | Learn JS Step by Step
JavaScript Basics for Beginners | Learn JS Step by Step
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
Javascript Fundamental
Javascript Fundamental
Javascript Cheet cheet
Javascript Cheet cheet
Private class properties
Private class properties
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Regular Expressions (Regex) Cheat Sheet
JavaScript Regular Expressions (Regex) Cheat Sheet
JavaScript Cheat Sheet 🚀 | Complete JS Reference for Beginners & Developers
JavaScript Cheat Sheet 🚀 | Complete JS Reference for Beginners & Developers
JavaScript Basics | Chapter 1 📖 | Part 1
JavaScript Basics | Chapter 1 📖 | Part 1
Javascript String Methods
Javascript String Methods
Javascript Loops & Iterations
Javascript Loops & Iterations
HTML,CSS JAVASCRIPT cheatsheet
HTML,CSS JAVASCRIPT cheatsheet
Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial
a poster with different types of projects and instructions for students to use in the classroom
a poster with different types of projects and instructions for students to use in the classroom
Learn JavaScript in the Right Order 🚀
Learn JavaScript in the Right Order 🚀
🚀 JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
🚀 JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
JavaScript Mindmap
JavaScript Mindmap
Complete Beginner CSS Master Notes
Complete Beginner CSS Master Notes
Java script string method
Java script string method

```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!

Related Articles

Nullish Coalescing Operator In Javascript W3schools W3schools Javascript Login Form Javascript Url Example Javascript Unicode Code Javascript Classes Tutorial Objects In Javascript In Web Technology Javascript Calculator Tutorial Javascript Array With Unique Objects Javascript Classes W3 W3schools Javascript Form Submit