有限状态机(Finite-state machine)

var menu = {
      
    // 当前状态
    currentState: 'hide',
  
    // 绑定事件
    initialize: function() {
      var self = this;
      self.on("hover", self.transition);
    },
  
    // 状态转换
    transition: function(event){
      switch(this.currentState) {
        case "hide":
          this.currentState = 'show';
          doSomething();
          break;
        case "show":
          this.currentState = 'hide';
          doSomething();
          break;
        default:
          console.log('Invalid State!');
          break;
      }
    }
  
  };

https://github.com/jakesgordon/javascript-state-machine

http://www.ruanyifeng.com/blog/2013/09/finite-state_machine_for_javascript.html

原文地址:https://www.cnblogs.com/human/p/3470930.html