Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. function Calculator () {
  2. this._currentValue = 0;
  3. this.commands = [];
  4. }
  5.  
  6. Calculator.prototype = {
  7. execute: function(command) {
  8. this._currentValue = command.execute(this._currentValue);
  9. this.commands.push(command);
  10. },
  11. undo: function() {
  12. var cmd = this.commands.pop();
  13.  
  14. this._currentValue = cmd.undo(this._currentValue);
  15. },
  16. getCurrentValue: function() {
  17. return this._currentValue;
  18. }
  19. }
  20.  
  21. function Command (fn, undo, value) {
  22. this.execute = fn;
  23. this.undo = undo;
  24. this.value = value;
  25. }
  26.  
  27. function add (value) {
  28. return value + this.value;
  29. }
  30. function AddCommand (value) {
  31. Command.call(this, add, sub, value);
  32. }
  33. function sub (value) {
  34. return value - this.value;
  35. }
  36. function SubCommand (value) {
  37. Command.call(this, sub, add, value);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement