singleton

 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
var $getInstance = function (class_object) {
var index = $getInstanceIndex(class_object);
if(index == -1) {
index = $insertInstance(class_object, new class_object());
}
return top.instances.objects[index];
}
var $getInstanceIndex = function (class_object) {
if(!top.instances) {
$clearInstances();
}
return top.instances.classes.indexOf(class_object);
}
var $insertInstance = function (class_object, object) {
top.instances.classes.push(class_object);
var index = top.instances.objects.push(null) - 1;
top.instances.objects[index] = object;
return index;
}
var $setInstance = function (class_object, object) {
var index = $getInstanceIndex(class_object);
if(index == -1){
index = $insertInstance(class_object, object);
}
return top.instances.objects[index] = object;
}
var $clearInstances = function() {
top.instances = { classes: [], objects: []};
}
var $clearInstance = function (class_object) {
var index = $getInstanceIndex(class_object);
delete top.instances.classes[index];
delete top.instances.objects[index];
}