-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSingleton.js
More file actions
45 lines (36 loc) · 919 Bytes
/
Copy pathSingleton.js
File metadata and controls
45 lines (36 loc) · 919 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
37
38
39
40
41
42
43
44
45
/**
* @return {Object}
*/
var Singleton = (function () {
/**
* @type {Object}
*/
var instance = null;
/**
* @return {Object}
*/
function init() {
// return the public interface of this object
return {
publicMethod: function () {
console.log('this is a public method');
},
publicProperty: 'public property'
};
}
// return the public interface of this singleton
return {
/**
* @returns {Object}
*/
getInstance: function () {
if (instance === null) {
instance = init();
}
return instance;
}
};
})();
var firstSingletonInstance = Singleton.getInstance();
var secondSingletonInstance = Singleton.getInstance();
console.log(firstSingletonInstance === secondSingletonInstance); // output: true