8.js模式-状态模式
1. 状态模式
var offLightState = function(light){
this.light = light;
}
- offLightState.prototype.buttonWasPressed = function(){
console.log('弱光');
this.light.setState(this.weakLightState);
}
var weakLightState = function(light){
this.light = light;
}
weakLightState.prototype.buttonWasPressed = function(){
console.log('弱光');
this.light.setState(this.strongLightState);
}
var strongLightState = function(light){
this.light = light;
}
strongLightState.prototype.buttonWasPressed = function(){
console.log('弱光');
this.light.setState(this.offLightState);
}
var light = function(){
this.offLightState = new offLightState(this);
this.weakLightState = new weakLightState(this);
this.strongLightState = new strongLightState(this);
this.button = null;
}
light.prototype.init = function(){
this.currentState = this.offLightState;
this.button.onclick = function(){
this.currentState.buttonWasPressed();
}
}
light.prototype.setState = function(state){
this.currentState = state;
}
var light = new light();
light.init();
- 7.js模式-装饰者模式
- 6.js模式-中介者模式
- 5.js模式-职责链模式
- 4.js模式-发布-订阅模式
- 3.js模式-策略模式
- 2.js模式-单例模式
- JavaScript基础——类型、值和变量
- javascript中onSubmit="return xxx()"的问题
- js验证函数摘录
- JavaScript基础知识
- JavaScript 数组
- 理解JavaScript中的事件轮询
- WebApi传递JSON参数 接收不到参数
- Gson解析JSON中动态未知字段key的方法
- [LeetCode][JavaScript]Path Sum II
- javascript动态添加css样式
- [LeetCode][JavaScript]Path Sum
- Javascript在页面的执行顺序
- Angular.js学习笔记(一)
- 转载js入门须知