Created
August 26, 2012 15:57
-
-
Save jmgunn87/3481297 to your computer and use it in GitHub Desktop.
js memento(undo redo)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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