Skip to content

Instantly share code, notes, and snippets.

@jmgunn87
Created August 26, 2012 15:57
Show Gist options
  • Save jmgunn87/3481297 to your computer and use it in GitHub Desktop.
Save jmgunn87/3481297 to your computer and use it in GitHub Desktop.
js memento(undo redo)
function Memento(originalState) {
this.state = originalState;
this.stack = new Array(this.state);
this.stackIndex = 0;
}
Memento.prototype.undo = function () {
return this.stackIndex > 0 ?
this.state = this.stack[--this.stackIndex]: this.state;
};
Memento.prototype.save = function (state) {
this.stack.length = ++this.stackIndex + 1;
return this.state = this.stack[this.stackIndex] = state;
};
Memento.prototype.redo = function () {
return this.stackIndex < this.stack.length - 1 ?
this.state = this.stack[++this.stackIndex] : this.state;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment