Skip to content

Instantly share code, notes, and snippets.

@krizpoon
Created May 20, 2015 04:28
Show Gist options
  • Save krizpoon/9b451a456b0df7cb604d to your computer and use it in GitHub Desktop.
Save krizpoon/9b451a456b0df7cb604d to your computer and use it in GitHub Desktop.
A history list object that behaves like browser's history
function HistoryList(home)
{
var arr = [home];
var ptr = 1;
this.push = function(elem) { arr.splice(ptr, arr.length-ptr, elem); ptr = arr.length; return this; }
this.moveTo = function(pos) { ptr = Math.min(Math.max(1, pos), arr.length); return this; }
this.move = function(steps) { return this.moveTo(ptr + steps); }
this.get = function() { return arr[ptr-1]; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment