-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingletonModule.js
65 lines (61 loc) · 1.44 KB
/
SingletonModule.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//单例模式
var mySingleton = (function(){
//实例保持了singleton的一个引用
var instance;
function init(){
//Singletn
//私有方法和变量
function privateMethod(){
console.log("I am private");
}
var privateVariable = "Im also private";
var privateRandomNumber = Math.random();
return {
//共有方法和变量
publicMethod:function(){
console.log("The public can see me !");
},
publicProperty:"Im also private",
getRandomNumber:function(){
return privateRandomNumber;
}
}
};
return {
//获取Singleton的实例
getInstance:function(){
if(!instance){
instance = init();
}
return instance;
}
};
})();
var myBadSingleton = (function(){
//实例保持了Singleton的一个引用
var instance;
function init(){
//Singleton
var privateRandomNumber = Math.random();
return {
getRandomNumber:function(){
return privateRandomNumber;
}
};
};
return {
//每次都创建新实例
getInstance:function(){
instance = init();
return instance;
}
}
})()
//单例输出
var singleA = mySingleton.getInstance();
var singleB = mySingleton.getInstance();
//丝黎话私有引用对象,调用引用对象方法实现
console.log(singleA.getRandomNumber() === singleB.getRandomNumber());//true
var badSingleA = myBadSingleton.getInstance();
var badSingleB = myBadSingleton.getInstance();
console.log(badSingleA.getRandomNumber()!==badSingleB.getRandomNumber());//true