-
Notifications
You must be signed in to change notification settings - Fork 1
/
module.js
34 lines (26 loc) · 916 Bytes
/
module.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
// The module pattern is based on the object literals
// Object Literals are basically hashes, dicionaries, or key-value pair collections
const myObjectLiteral = {
variableKey: 1,
functionKey: () => console.log("Something")
};
myObjectLiteral.functionKey();
// The idea of the module pattern is to provide both public and private encapsulation
const namespace = (() => {
let privateVariable = 0;
const privateMethod = () =>
console.log(`Private Variable: ${privateVariable}`);
return {
publicVariable: 1,
publicaMethod: () => {
privateVariable++;
privateMethod();
}
};
})();
console.log(namespace.publicVariable);
namespace.publicaMethod();
namespace.publicaMethod();
// In the module pattern we can create real private variables and methods.
// In this case, only our module can access the private attributes.
// It is not accessible for the rest of the application.