当前位置:网站首页>(JS)状态模式
(JS)状态模式
2022-06-29 09:57:00 【不愿透露姓名的余菜鸟】
(JS)状态模式
每次状态变化都会触发逻辑
不能总是使用if…else判断
class State {
constructor(color) {
this.color = color;
}
handle() {
console.log(`turn to ${
this.color} light`);
context.setState(this);
}
}
class Context {
constructor() {
this.state = null;
}
getState() {
return this.state;
}
setState(state) {
this.state = state;
}
}
let context = new Context();
let green = new State("green");
let red = new State("red");
let yellow = new State("yellow");
green.handle();
red.handle();
yellow.handle();
console.log(context.getState());
turn to green light
turn to red light
turn to yellow light
State {
color: "yellow"}
边栏推荐
- BUUCTF RE-easyre
- 30-year-old female, ordinary software testing Yuanyuan, confused and anxious about her career
- PyTorch学习笔记(6)——DataLoader源代码剖析
- FreeRTOS porting of official website based on keil5 auto configuring STM32F103 standard library
- ZABBIX monitors various MySQL indicators
- 当技术人成长为 CEO,应该修改哪些“Bug”?
- 全面理解MESI缓存一致性协议
- VI退出 退出VIM 适用新手
- Excel日期及数字格式处理
- np. astype()
猜你喜欢

Exemples de programmation stm32f1 et stm32cubeide - entraînement du capteur de portée ultrasonique

Redis installation and cluster setup under Linux

Software test model (V model and W model)

Daily question brushing record (VII)

当技术人成长为 CEO,应该修改哪些“Bug”?

He was in '98. I can't play with him

Reids设计与实现

Free books! The best-selling book "Introduction and practice of OpenCV image processing" has been completed

深入浅出总结Flink运行时架构

STM32F1与STM32CubeIDE编程实例-超声波测距传感器驱动
随机推荐
Reids设计与实现
Real test = "half product + Half development"?
ZABBIX monitors various MySQL indicators
arcgis创建postgre企业级数据库
SQL Server 数据库增删改查语句
常见电机分类和驱动原理动画[通俗易懂]
Common motor classification and driving principle animation [easy to understand]
JS post download file
VI退出 退出VIM 适用新手
各位大佬 请教下mysqlcdc的数据不支持开窗函数吗 像row_number ,lead这种
Dropout layer
PyTorch学习笔记(6)——DataLoader源代码剖析
WinForm uses zxing to generate QR code
Buuctf-- happy New Year
USB转RS485串口电路设计「建议收藏」
Given the values of two integer variables, the contents of the two values are exchanged (C language)
《Datawhale推荐系统教程》来了!
反CSRF爆破的三种姿势
Ningde era Kirin battery has greater ambition
(JS)数组去除重复