File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ One place JavaScript!
4
4
5
5
### Table of Contents
6
6
7
+ - [ Design Pattern] ( #pattern )
7
8
- [ Callback/ Higher-order Function] ( #callback )
8
9
- [ Promises] ( #promises )
9
10
- [ Closures] ( #closures )
@@ -12,6 +13,57 @@ One place JavaScript!
12
13
- [ DOM] ( #dom )
13
14
- [ Equality comparisons] ( #eqality )
14
15
16
+ <a name =“pattern”/ >
17
+
18
+ #### Design Pattern
19
+
20
+ - [ Singleton Pattern] ( #singleton )
21
+
22
+
23
+ <a name =“singleton”/ >
24
+
25
+ ##### Singleton Pattern:
26
+ The singleton design pattern restricts the instantiation of a function/class to one variable/object.
27
+
28
+ Uses:
29
+ 1 . When exactly one variable/object is needed to coordinate actions across the system.
30
+ 2 . Reduce the need of global variables.
31
+ 3 . The getInstance method demonstates another design pattern called Lazy Load.
32
+
33
+ ``` javascript
34
+ var Singleton = (function () {
35
+ var instance;
36
+
37
+ function createInstance () {
38
+ var object = new Object (" I am the instance" );
39
+ return object;
40
+ }
41
+
42
+ return {
43
+ getInstance : function () {
44
+ if (! instance) {
45
+ instance = createInstance ();
46
+ }
47
+ return instance;
48
+ }
49
+ };
50
+ })();
51
+
52
+ function run () {
53
+
54
+ var instance1 = Singleton .getInstance ();
55
+ var instance2 = Singleton .getInstance ();
56
+
57
+ alert (" Same instance? " + (instance1 === instance2)); // Output: Same instance? true
58
+ }
59
+ ```
60
+
61
+ Ref:
62
+
63
+ https://blog.mgechev.com/2014/04/16/singleton-in-javascript/
64
+
65
+ https://www.dofactory.com/javascript/singleton-design-pattern
66
+
15
67
<a name =“callback”/ >
16
68
17
69
#### Callback/ Higher-order Function
You can’t perform that action at this time.
0 commit comments