-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleton.js
More file actions
36 lines (30 loc) · 755 Bytes
/
Singleton.js
File metadata and controls
36 lines (30 loc) · 755 Bytes
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
26
27
28
29
30
31
32
33
34
35
36
class Counter
{
constructor()
{
if(typeof Counter.instance == 'object')
{
return Counter.instance;
}
this.count = 0;
Counter.instance = this;
return this;
}
getCounter()
{
return this.count;
}
increaseCounter()
{
return this.count++;
}
}
const myCount1 = new Counter();
const myCount2 = new Counter();
myCount1.increaseCounter();
myCount1.increaseCounter();
myCount2.increaseCounter();
myCount2.increaseCounter();
console.log("the number is "+myCount1.getCounter());
console.log("the number is "+myCount2.getCounter());
//Link of tuto is https://www.youtube.com/watch?v=GrYs0qDQEp0&list=PLNkWIWHIRwMGzgvuPRFkDrpAygvdKJIE4&index=2