Sling Academy
Home/JavaScript/Extracting Common Behaviors into Reusable JavaScript Classes

Extracting Common Behaviors into Reusable JavaScript Classes

Last updated: December 12, 2024

Software development often involves repeating certain patterns and behaviors across multiple components or sections of an application. To keep code manageable, efficient, and easy to update, we can extract these common behaviors into reusable JavaScript classes.

By applying Object-Oriented Programming (OOP) principles in JavaScript, we can create classes that encapsulate these behaviors, allowing us to maintain DRY (Don't Repeat Yourself) principles within our codebase.

Understanding the Basics of JavaScript Classes

JavaScript classes were introduced in ECMAScript 2015 (ES6) as syntactical sugar over JavaScript's existing prototype-based inheritance. Classes provide a clear and readable syntax for creating objects and dealing with inheritance.

Here's a simple example of a JavaScript class:

class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(`${this.name} makes a noise.`);
  }
}

Extracting Common Behaviors

Let's say you are developing a web application that tracks different types of vehicles, and you want to extract the common behaviors such as starting and stopping the engine. Here’s how you can leverage classes:

Step 1: Define a Base Class

Create a base class named Vehicle that will contain the common methods:

class Vehicle {
  constructor(type) {
    this.type = type;
    this.isEngineOn = false;
  }

  startEngine() {
    if (!this.isEngineOn) {
      this.isEngineOn = true;
      console.log(`${this.type} engine started.`);
    } else {
      console.log(`${this.type} engine is already on.`);
    }
  }

  stopEngine() {
    if (this.isEngineOn) {
      this.isEngineOn = false;
      console.log(`${this.type} engine stopped.`);
    } else {
      console.log(`${this.type} engine is already off.`);
    }
  }
}

By defining common functionalities like startEngine and stopEngine, we ensure that every vehicle instance has a consistent way to handle these actions.

Step 2: Extend the Base Class for Specific Behavior

Now, let’s create two specific types of vehicles, a car and a motorcycle, each inheriting from the Vehicle base class, but possibly expanding upon it:

class Car extends Vehicle {
  constructor() {
    super('Car');
  }

  openTrunk() {
    console.log('Trunk is opened.');
  }
}

class Motorcycle extends Vehicle {
  constructor() {
    super('Motorcycle');
  }

  popWheelie() {
    console.log('Popping a wheelie!');
  }
}

Now you can create car or motorcycle objects that can reuse the startEngine and stopEngine methods from the Vehicle class while having their own specialized methods.

Step 3: Implementing and Testing

Using the classes created, you can instantiate objects and test the functionality:

const myCar = new Car();
myCar.startEngine(); // Outputs: Car engine started.
myCar.openTrunk();   // Outputs: Trunk is opened.

const myMotorcycle = new Motorcycle();
myMotorcycle.startEngine(); // Outputs: Motorcycle engine started.
myMotorcycle.popWheelie(); // Outputs: Popping a wheelie!

The objects myCar and myMotorcycle both have access to shared and specialized functionalities efficiently without code duplication.

Benefits of Using Reusable Classes

There are several advantages to utilizing reusable classes in JavaScript:

  • Scalability: With reusable classes, adding new features or extending functionalities becomes easier and more predictable.
  • Maintainability: Code becomes more organized, which simplifies debugging and updating your logic.
  • Efficiency: By reusing code, you significantly reduce the chance of errors and redundancy.

In conclusion, harnessing JavaScript classes for extracting common behaviors leads to better-managed projects that are adaptable and scalable over time. Developers can focus more on innovating than managing code duplicates, attentively organizing their architectures for performance and simplicity.

Next Article: Enhancing Performance by Structuring Logic in JavaScript Classes

Previous Article: Redefining Code Boundaries Through JavaScript Class Hierarchies

Series: JavaScript Classes

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration