用js的class形式写一段单例的状态管理器
时间: 2024-05-19 12:14:18 浏览: 46
class SingletonStateManager {
constructor() {
if (SingletonStateManager.instance) {
return SingletonStateManager.instance;
}
SingletonStateManager.instance = this;
this.state = {};
}
setState(key, value) {
this.state[key] = value;
}
getState(key) {
return this.state[key];
}
}
const stateManager = new SingletonStateManager();
export default stateManager;
阅读全文