Building Reusable Components

Tutorial 4 of 5

Building Reusable Components

1. Introduction

In this tutorial, we will explore how to build reusable components using JavaScript Object Oriented Programming (OOP). The aim is to help you understand how to design and implement components that can be reused across different parts of your application, thereby reducing code redundancy and improving maintainability.

By the end of this tutorial, you will learn:
- How to define and use classes in JavaScript.
- How to create reusable components using JavaScript OOP.

Prerequisites: Basic understanding of JavaScript and HTML is required.

2. Step-by-Step Guide

In JavaScript, we can create reusable components using classes. A class is a blueprint for creating objects. It encapsulates data and operations that are related to a certain type of object.

Defining a Class

A class is defined using the class keyword, followed by the name of the class. The name of the class is usually capitalized.

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

In the example above, Component is a class with a constructor. The constructor is a special method that is called when an object is created from a class. It initializes the object's properties.

Creating an Object

To create an object, we use the new keyword, followed by the class name and parentheses.

let component = new Component("Button");

In this case, we created a new Component object named component.

3. Code Examples

Example 1: Creating and using a component

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

  display() {
    console.log(`Component: ${this.name}`);
  }
}

let component = new Component("Button");
component.display(); // Output: Component: Button

In this example, we added a display method to the Component class. This method prints the name of the component to the console.

Example 2: Extending a component

In JavaScript, we can create a new class that inherits from an existing class using the extends keyword. This is known as class inheritance or subclassing.

class Button extends Component {
  constructor(name, color) {
    super(name);
    this.color = color;
  }

  display() {
    console.log(`Button: ${this.name}, Color: ${this.color}`);
  }
}

let button = new Button("Submit", "Blue");
button.display(); // Output: Button: Submit, Color: Blue

In this example, Button is a subclass of Component. It inherits the properties and methods of Component, and adds a new property (color) and overrides the display method.

4. Summary

In this tutorial, we have learned how to build reusable components using JavaScript OOP. We've covered how to define and use classes, create objects, and extend classes to create more specific components.

For further learning, you can explore more about JavaScript OOP, such as private properties and methods, static properties and methods, and getter/setter methods. Here are a few resources:
- JavaScript.info: Object-oriented programming
- MDN: Introduction to Object-Oriented JavaScript

5. Practice Exercises

Exercise 1:

Create a Checkbox class that extends from Component. It should have a checked property that can be true or false, and a toggle method that changes the value of checked.

Solution:

class Checkbox extends Component {
  constructor(name, checked) {
    super(name);
    this.checked = checked;
  }

  toggle() {
    this.checked = !this.checked;
  }
}

let checkbox = new Checkbox("Remember me", false);
checkbox.toggle();
console.log(checkbox.checked); // Output: true

Exercise 2:

Create a RadioGroup class that has a list of Radio components. Each Radio component should have a selected property. Only one Radio component can be selected at a time.

Solution:

class Radio extends Component {
  constructor(name, selected) {
    super(name);
    this.selected = selected;
  }
}

class RadioGroup {
  constructor(radios) {
    this.radios = radios;
  }

  select(name) {
    for (let radio of this.radios) {
      radio.selected = radio.name === name;
    }
  }
}

let radioGroup = new RadioGroup([
  new Radio("Option 1", false),
  new Radio("Option 2", false),
  new Radio("Option 3", false)
]);

radioGroup.select("Option 2");
for (let radio of radioGroup.radios) {
  console.log(`Radio: ${radio.name}, Selected: ${radio.selected}`);
}

In the solutions above, we've created and manipulated different types of components. Practice creating different types of components and manipulating them to reinforce your understanding.