-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject2.js
More file actions
34 lines (24 loc) · 776 Bytes
/
Copy pathobject2.js
File metadata and controls
34 lines (24 loc) · 776 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
// Object by constructor
// A Singleton is an object that is instantiated only once and provides a single shared instance throughout the application.
const TinderUserSingleton = (function () {
let instance;
function createInstance() {
return { name: "Singleton User" };
}
return {
getInstance: function () {
if (!instance) {
instance = createInstance();
}
return instance;
}
};
})();
const user1 = TinderUserSingleton.getInstance();
const user2 = TinderUserSingleton.getInstance();
console.log(user1 === user2); // true (Same instance)
// Non singeton
const tinderUser = new Object();
const obj1 = new Object();
const obj2 = new Object();
console.log(obj1 === obj2); // false (Different instances)