create a browser history
2022年5月1日小于 1 分钟
create a browser history
Question
I believe you are very familiar about your browser you are currently visiting https://bigfrontend.dev with.
The common actions relating to history are:
new BrowserHistory()
- when you open a new tab, it is set with an empty historygoBack()
- go to last entry, notice the entries are kept so thatforward()
could get us backforward()
- go to next visited entryvisit()
- when you enter a new address or click a link, this adds a new entry but truncate the entries which we couldforward()
to.
Say we start a new tab, this is the empty history.
[ ]
Then visit url A, B, C in turn.
[ A, B, C]
↑
We are currently at C, we could goBack()
to B, then to A
[ A, B, C]
↑
forward()
get us to B
[ A, B, C]
↑
Now if we visit a new url D, since we are currently at B, C is truncated.
[ A, B, D]
↑
You are asked to implement a BrowserHistory
class to mimic the behavior.
Code
class BrowserHistory {
/**
* @param {string} url
* if url is set, it means new tab with url
* otherwise, it is empty new tab
*/
constructor(url) {
}
/**
* @param { string } url
*/
visit(url) {
}
/**
* @return {string} current url
*/
get current() {
}
// go to previous entry
goBack() {
}
// go to next visited url
forward() {
}
}