Browser History Tracker: A Simplified Version
Understanding how browser history works can be intriguing. Browsers are quite efficient at keeping a tab of our actions. Today, we'll create a simplified model of a browser history tracker. This tracker will maintain the sequence of visited pages (URLs) and allow us to navigate forward and backward in history. 🌐
Class Structure and Initial Setup
Let's begin by laying down the groundwork for our Browser History class. We'll need two main properties:
- History Array: To keep track of all the visited URLs.
- Pointer: An integer that points to the current URL we're viewing in the history array.
Here's the initial class setup with a constructor:
JAVASCRIPT
1class BrowserHistory {
2 constructor(url) {
3 this.history = [];
4 this.pointer = -1;
5 if (url !== undefined) {
6 this.history.push(url);
7 }
8 }
9}
What's happening here?
- The constructor initializes an empty
history
array and sets thepointer
to -1. - If a URL is provided during the instantiation, it gets added to the
history
.