-
Notifications
You must be signed in to change notification settings - Fork 0
/
NamespacingAndModule pattern in Javascript
63 lines (63 loc) · 1.23 KB
/
NamespacingAndModule pattern in Javascript
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
//1. Static Namespacing
//object Notation.
var myApp = {
id: 0,
next: function () {
return this.id++;
},
reset: function () {
this.id = 0;
}
}
window.console && console.log(myApp.next(), myApp.next(), myApp.reset(), myApp.next()
)
//0, 1, undefined, 0
//Module Pattern
var myApp = (function () {
var id = 0;
return {
next: function () {
return id++;
},
reset: function () {
id = 0;
}
};
}) ();
window.console && console.log(myApp.next(), myApp.next(), myApp.reset(), myApp.next()
)
//0, 1, undefined, 0
//2. Dynamic Namespacing
var myApp = {
};
(function (context) {
var id = 0;
context.next = function () {
return id++;
};
context.reset = function () {
id = 0;
}
}) (this);
console.log(next(), next(), reset(), next()
)
//0, 1, undefined, 0
//diff modules act independently.
var subsys1 = {
},
subsys2 = {
};
var nextIdMod = function (startId) {
var id = startId || 0;
this.next = function () {
return id++;
};
this.reset = function () {
id = 0;
}
};
nextIdMod.call(subsys1);
nextIdMod.call(subsys2, 1000);
window.console && console.log(subsys1.next(), subsys1.next(), subsys2.next(), subsys1.reset(), subsys2.next(), subsys1.next()
)
//0, 1, 1000, undefined, 1001, 0