-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingelton.js
More file actions
36 lines (25 loc) · 713 Bytes
/
singelton.js
File metadata and controls
36 lines (25 loc) · 713 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
// The Singleton Pattern limits the number of instances of a particular object to just one.
// http://www.dofactory.com/javascript/singleton-design-pattern
// The instances in clouser. (preffer)
function Singelton() {
var instance = this;
Singelton = function() {
return instance
};
Singelton.prototype = this;
instance = new Singelton();
instance.constructor = Singelton;
return instance;
}
var x = new Singelton();
x.someProp = true;
var y = new Singelton();
y.someProp = false;
x.someProp // false
// The instances in a static prop.
function Singelton() {
if( typeof Singelton.instance == 'object') {
return Singelton.instance
}
Universe.instance = this;
}