-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleton.js
25 lines (21 loc) · 852 Bytes
/
Singleton.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Single {
constructor(color) {
// Single.instance is already created then returning that and not creating any new instance
if (Single.instance instanceof Single) {
return Single.instance;
}
this.settingsObj = {
color: color,
version: Math.floor(Math.random() * 100)
}
Object.freeze(this); //freeze prevents new properties from being added to it; prevents existing properties from being removed; and prevents existing properties, or their enumerability, configurability, or writability, from being changed
Single.instance = this; //storing first instance of this Singleton obj in Singleton.instance
}
get(key) {
return this[key];
}
}
let a = new Single("red");
let b = new Single("blue");
console.log("a", a)
console.log("b", b)