zoukankan      html  css  js  c++  java
  • js版本状态模式

    var Light = function(){
      this.currState = FSM.off; // 设置当前状态
      this.button = null;
    };
    Light.prototype.init = function(){
      var button = document.createElement( 'button' ),
      self = this;
      button.innerHTML = '已关灯';
      this.button = document.body.appendChild( button );
      this.button.onclick = function(){
        self.currState.buttonWasPressed.call( self ); // 把请求委托给 FSM 状态机
      };
    };
    var FSM = {
      off: { 
        buttonWasPressed: function(){
        console.log( '关灯' );
        this.button.innerHTML = '下一次按我是开灯';
        this.currState = FSM.on;
        }
      },
      on: {
    
        buttonWasPressed: function(){
        console.log( '开灯' );
        this.button.innerHTML = '下一次按我是关灯';
        this.currState = FSM.off;
         }
      }
    };
    var light = new Light();
    light.init();

     摘自JavaScript设计模式与开发实践

  • 相关阅读:
    [POI2014]KUR-Couriers
    MySQL有哪些索引
    索引的设计原则
    explain参数之extra
    explain参数之type
    explain参数之select_type
    如何查询最后一行的记录
    为什么MySQL自增id不连续?
    MySQL字符集
    MySQL有哪些优化策略?
  • 原文地址:https://www.cnblogs.com/cszdsb/p/6652537.html
Copyright © 2011-2022 走看看