标签:logs 闭包 tee 例子 sel 执行 通过 fsm 环境
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();
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();
标签:logs 闭包 tee 例子 sel 执行 通过 fsm 环境
原文地址:http://www.cnblogs.com/meiyh/p/6515477.html