-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule.ts
More file actions
101 lines (88 loc) · 2.44 KB
/
module.ts
File metadata and controls
101 lines (88 loc) · 2.44 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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
var myModule = {
myProperty: "someValue",
// e.g we can define a further object for module configuration:
myConfig: {
useCaching: true,
language: "en"
},
// a very basic method
saySomething: function () {
console.log("Where in the world is Paul Irish today?");
},
// output a value based on the current configuration
reportMyConfig: function () {
console.log("Caching is: " + (this.myConfig.useCaching ? "enabled" : "disabled"));
},
// override the current configuration
updateMyConfig: function (newConfig) {
if (typeof newConfig === "object") {
this.myConfig = newConfig;
}
}
};
myModule.saySomething(); // Outputs: Where in the world is Paul Irish today?
myModule.reportMyConfig(); // Outputs: Caching is: enabled
myModule.updateMyConfig({
language: "fr",
useCaching: false
});
myModule.reportMyConfig(); // Outputs: Caching is: disabled
// closure Example -
var testModule = (function () {
// private
var counter = 0;
// public
return {
incrementCounter: function () {
return counter++;
},
resetCounter: function () {
console.log("counter value prior to reset: " + counter);
counter = 0;
console.log("counter value after reset: " + counter);
}
};
})();
// Increment our counter
testModule.incrementCounter();
// Check the counter value and reset
testModule.resetCounter();
// example
var modularpattern = (function () {
// your module code goes here
var sum = 0;
return {
add: function () {
sum = sum + 1;
return sum;
},
reset: function () {
return sum = 0;
}
}
}());
console.log(modularpattern.add()); // alerts: 1
console.log(modularpattern.add()); // alerts: 2
console.log(modularpattern.reset()); // alerts: 0
// example
const sum = (num1, num2) => num1 + num2;
const result = sum(20, 30); // 50
console.log(result);
// example
const globalData = {
x: 20
};
const myModule2 = (function(global) { // <- access injections
// private stuff in the module
const val = 10 + global.x;
// expose what you want
return {
prop: 12,
method() {
return val;
}
}
})(globalData); // <- inject into your module
console.log(myModule2.prop); // prints 12
console.log(myModule2.method()); // prints 30
// node module.ts