-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser-history.js
More file actions
48 lines (37 loc) · 782 Bytes
/
browser-history.js
File metadata and controls
48 lines (37 loc) · 782 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class BrowserHistory {
constructor(url) {
this.history = [url];
this.urlPointer = 0;
}
visit(url) {
this.urlPointer++;
this.history = this.history.slice(0, this.urlPointer);
this.history.push(url);
return url;
}
back() {
if(this.urlPointer === 0) {
return null;
}
this.urlPointer--;
return this.history[this.urlPointer];
}
forward() {
if(this.urlPointer === this.history.length - 1) {
return null;
}
this.urlPointer++;
return this.history[this.urlPointer];
}
}
const history = new BrowserHistory("A");
history.visit('B')
history.visit('C')
history.visit('D')
history.back()
console.log(history.back()) // 'B'
history.visit('E')
history.visit('F')
console.log(history.history)
history.back()
console.log(history.back()); // 'B'