-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasic Module Pattern.js
71 lines (53 loc) · 1.46 KB
/
Basic Module Pattern.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
66
67
68
69
70
71
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
#DEMONSTRATING THE BASIC MODULE PATTERN
//dom is a simple object
//dom has a public "_counter" variable.
var dom = {
_counter: 0,
generateId: function() {
return "customId" + this._counter++;
},
create: function(tagName, id) {
var el = document.createElement(tagName);
el.id = id || this.generateId();
return el;
},
};
//NOW dom is an IIFE
//dom is a "module" with EVERYTHING being private
//ALMOST copy and paste from dom-object to dom-module
var dom = (function() {
//change this._counter to var _counter
var _counter: 0;
//returning an object so that these methods are available.
return {
generateId: function() {
return "customId" + _counter++;
},
create: function(tagName, id) {
var el = document.createElement(tagName);
el.id = id || generateId();
return el;
},
}
})();
//module pattern - alternate version.
//no pros, no cons
//just a differnt way of writing it
var dom = (function() {
//change this._counter to var _counter
var _counter: 0;
var generateId = function() {
return "customId" + _counter++;
};
var create = function(tagName, id) {
var el = document.createElement(tagName);
el.id = id || generateId();
return el;
};
//returning an object so that these methods are available.
return {
generateId:generateId,
create:create
};
})();