Learn to build a Todo App using Object-Oriented Programming in JavaScript. Understand class structures, methods, and how to integrate a user interface.
Welcome to an exciting chapter where we will apply Object-Oriented Programming (OOP) concepts to build a practical and interactive Todo App using JavaScript. This project will not only solidify your understanding of classes and objects but also demonstrate how OOP can enhance code organization and scalability.
Before we dive into the code, let’s outline what we aim to achieve with this project:
To get started, ensure you have a basic HTML file to serve as the user interface. We’ll use this to interact with our JavaScript code. Here’s a simple setup:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Todo App</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="app">
<h1>Todo List</h1>
<input type="text" id="taskInput" placeholder="Enter a new task">
<button id="addTaskButton">Add Task</button>
<ul id="taskList"></ul>
</div>
<script src="app.js"></script>
</body>
</html>
Let’s begin by defining the core classes for our Todo app. We’ll start with a Task
class to represent individual tasks and a TaskManager
class to handle task operations.
The Task
class will encapsulate properties and methods related to a single task. Here’s how you can define it:
class Task {
constructor(description) {
this.description = description;
this.completed = false; // Default state of a task
}
toggleCompletion() {
this.completed = !this.completed;
}
}
completed
status of false
.The TaskManager
class will manage a collection of tasks, providing methods to add, remove, and display tasks.
class TaskManager {
constructor() {
this.tasks = [];
}
addTask(description) {
const task = new Task(description);
this.tasks.push(task);
}
removeTask(index) {
this.tasks.splice(index, 1);
}
getTasks() {
return this.tasks;
}
}
Task
and adds it to the list.Now that we have our classes, let’s integrate them with the HTML interface to allow user interaction.
We’ll add event listeners to handle user actions, such as adding and removing tasks.
const taskInput = document.getElementById('taskInput');
const addTaskButton = document.getElementById('addTaskButton');
const taskList = document.getElementById('taskList');
const taskManager = new TaskManager();
addTaskButton.addEventListener('click', () => {
const description = taskInput.value.trim();
if (description) {
taskManager.addTask(description);
taskInput.value = '';
renderTasks();
}
});
function renderTasks() {
taskList.innerHTML = '';
taskManager.getTasks().forEach((task, index) => {
const li = document.createElement('li');
li.textContent = task.description;
li.addEventListener('click', () => {
task.toggleCompletion();
renderTasks();
});
const removeButton = document.createElement('button');
removeButton.textContent = 'Remove';
removeButton.addEventListener('click', () => {
taskManager.removeTask(index);
renderTasks();
});
li.appendChild(removeButton);
taskList.appendChild(li);
});
}
Now that you have a basic Todo app, consider adding more features to enhance its functionality:
To better understand the flow of task management, let’s use a flowchart to visualize the process.
flowchart TD A[Start] --> B[User Enters Task] B --> C{Is Description Valid?} C -- Yes --> D[Add Task to TaskManager] C -- No --> E[Prompt User for Valid Input] D --> F[Render Task List] E --> B F --> G[User Clicks Task] G --> H[Toggle Task Completion] H --> F G --> I[User Clicks Remove] I --> J[Remove Task from TaskManager] J --> F
This flowchart illustrates how tasks are added, toggled, and removed within the application.
By using OOP principles, our Todo app benefits from:
Task
class, making it easier to manage and modify.TaskManager
class separates task management logic from the user interface, promoting clean and organized code.Now it’s your turn! Experiment with the code by:
Before we wrap up, let’s review some key concepts:
Task
class?TaskManager
class manage tasks?In this section, we’ve built a functional Todo app using JavaScript classes, demonstrating how OOP can enhance code structure and maintainability. By encapsulating task-related logic within classes, we’ve created a scalable and organized codebase that can be easily extended with new features.
Remember, this is just the beginning. As you progress, you’ll build more complex and interactive applications. Keep experimenting, stay curious, and enjoy the journey!