-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton.js
More file actions
92 lines (74 loc) · 1.52 KB
/
singleton.js
File metadata and controls
92 lines (74 loc) · 1.52 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* @file 单例模式
* @author tuanfe
* @date 2014/5/19
*/
/**
* 实现方式一:定义一个字面量的形式创建一个对象,就可以认为是一个单体
*/
var singleton = {
name: 'tuanfe',
age: 25,
height: 175
}
/**
* 实现方式二:构造函数的方式
*
*/
function Singleton(){
//正常的属性
this.name= 'tuanfe';
this.age= 25;
if(typeof Singleton.instance === "object"){
return Singleton.instance;
}
//缓存this
Singleton.instance = this;
return this;
}
var s1 = new Singleton();
var s2 = new Singleton();
console.log(s1 === s2); //true
/**
* 实现方式三: 立即执行函数方式
*
*/
var Singleton;
(function(){
var instance;
Singleton = function Singleton(){
if(instance){
return instance;
}
instance = this;
//正常的属性
this.name= 'tuanfe';
this.age= 25;
}
})();
var s1 = new Singleton();
var s2 = new Singleton();
console.log(s1 === s2); //true
/**
* 实现方式四:闭包
*
*/
var Singleton = (function(){
//实例引用
var instance;
function init(){
//对象初始化操作
}
return {
//暴露出公共API,提供全局唯一访问点
getInstance: function(){
if(!instance){
instance = init();
}
return instance;
}
}
})();
var s1 = Singleton.getInstance();
var s2 = Singleton.getInstance();
console.log(s1 ===s2); //true