-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodules.js
More file actions
45 lines (35 loc) · 1.19 KB
/
modules.js
File metadata and controls
45 lines (35 loc) · 1.19 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
// Javascript functions let you create modules
// which help prevent naming conflicts
// Define a function which instantiates an object, its public and
// private methods, and immediately execute it (it returns the object)
NAMESPACE.module = (function () {
var privateVariable;
function privateFunction(x) {
// do something with x and privateVariable
}
return {
// public functions!
firstMethod: function (a, b) {
// privateVariable has scope here, not just a & b
},
secondMethod: function (c) {
// you can call privateFunction here as well
}
};
}());
// Or, a variation on the above could look like this:
(function() {
var privateVariable;
function privateFunction(x) {
// do something with x and privateVariable
}
GLOBAL.firstMethod = function (a, b) {
// privateVariable has scope here, not just a & b
};
GLOBAL.secondMethod = function (c) {
// you can call privateFunction here as well
};
}());
// Please note that in this second example, the outermost parentheses
// are REQUIRED in order for it to be a function expression and not just
// a function statement.