Singleton

Singleton

七月 31, 2019

Singleton is a simple and common Javascript Pattern.

Singleton implement sample

Instantial a mobx store

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class UserStore {
@observable name: string = "jingsong";
@action
updateName(name: string) {
this.name = name;
}
}
function instantiate(cons, ...args) {
if (!cons.instance) {
cons.instance = new cons(...args);
}
return cons.instance;
}
instantiate(UserStore);