JS模式--狀態模式(狀態機器)

來源:互聯網
上載者:User

標籤:logs   閉包   tee   例子   sel   執行   通過   fsm   環境   

  • 下面的狀態機器選擇通過Function.prototype.call方法直接把請求委託給某個字面量對象來執行。
 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);            };        };        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();
  •  下面利用delegate函數來重寫一遍這個狀態機器
  • 這是物件導向和閉包互換的一個例子。前者將變數儲存為對象的屬性,後者將變數封閉在閉包形成的環境中:
var delegate = function (client, delegation) {            return {                buttonWasPressed: function () {                    return delegation.buttonWasPressed.apply(client, arguments);                }            };        };        var light = function () {            this.offstate = delegate(this, FSM.off);            this.onstate = delegate(this, FSM.on);            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);            };        };        var FSM = {            off: {                buttonWasPressed: function () {                    console.log(‘關燈‘);                    this.button.innerHTML = ‘下一次按我是開燈‘;                    this.currstate = this.onstate;                }            },            on: {                buttonWasPressed: function () {                    console.log(‘開燈‘);                    this.button.innerHTML = ‘下一次按我是關燈‘;                    this.currstate = this.offstate;                }            }        };        var light = new light();        light.init();

 

JS模式--狀態模式(狀態機器)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.