Skip to content

Commit 89cd4f4

Browse files
author
Badacadabra
committed
Add Revealing Module (ES5 + ES6 + CoffeeScript + TypeScript)
1 parent 2f60c21 commit 89cd4f4

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict'
2+
3+
me = do ->
4+
# Public variables
5+
firstName = "Baptiste"
6+
lastName = "Vannesson"
7+
8+
# Private variable
9+
secretNickname = "Bada"
10+
11+
# Public functions
12+
sayHello = -> "Hello, #{firstName} #{lastName}!"
13+
getSecretNickname = -> secretNickname
14+
15+
# Revealed module
16+
firstName: firstName
17+
lastName: lastName
18+
sayHello: sayHello
19+
getSecretNickname: getSecretNickname
20+
21+
console.log me.firstName, me.lastName # OK
22+
console.log me.sayHello() # OK
23+
console.log me.secretNickname # Oops! Undefined!
24+
console.log me.getSecretNickname() # OK
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
var me = (function () {
4+
// Public variables
5+
var firstName = "Baptiste",
6+
lastName = "Vannesson";
7+
8+
// Private variable
9+
var secretNickname = "Bada";
10+
11+
// Public functions
12+
function sayHello() {
13+
return "Hello, " + firstName + " " + lastName + "!";
14+
}
15+
16+
function getSecretNickname() {
17+
return secretNickname;
18+
}
19+
20+
// Revealed module
21+
return {
22+
firstName: firstName,
23+
lastName: lastName,
24+
sayHello: sayHello,
25+
getSecretNickname: getSecretNickname,
26+
};
27+
})();
28+
29+
console.log(me.firstName, me.lastName); // OK
30+
console.log(me.sayHello()); // OK
31+
console.log(me.secretNickname); // Oops! Undefined!
32+
console.log(me.getSecretNickname()); // OK
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const me = (() => {
2+
// Public variables
3+
const firstName = "Baptiste",
4+
lastName = "Vannesson";
5+
6+
// Private variable
7+
const secretNickname = "Bada";
8+
9+
// Public functions
10+
const sayHello = () => `Hello, ${firstName} ${lastName}!`;
11+
const getSecretNickname = () => secretNickname;
12+
13+
// Revealed module
14+
return { firstName, lastName, sayHello, getSecretNickname };
15+
})();
16+
17+
console.log(me.firstName, me.lastName); // OK
18+
console.log(me.sayHello()); // OK
19+
console.log(me.secretNickname); // Oops! Undefined!
20+
console.log(me.getSecretNickname()); // OK

misc/RevealingModule/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Problem
2+
3+
JavaScript is not very strict when it comes to encapsulation and each script tends to pollute the global namespace. This is because the language has been built with global variables in mind.
4+
5+
# Solution
6+
7+
The Revealing Module pattern is a great solution to encapsulate some code. The idea is to store in a variable the returned expression of an IIFE (Immediately-Invoked Function Expression), which is generally an object literal that exposes all public members of the module. Within the IIFE, the scope is private. We can then declare private variables and functions that will be invisible from the outside. Conceptually, a module is a bit like a class.
8+
9+
N.B. In practice, this pattern is not often used anymore. CommonJS, AMD, or built-in ES6 modules are much better solutions nowadays... However, the legacy of this pattern is still visible in ES5 code, especially in CommonJS modules where module.exports exposes public members using an object literal.
10+
11+
```javascript
12+
// CommonJS (Revealing) Module exports
13+
module.exports = {
14+
firstName: firstName,
15+
lastName: lastName,
16+
sayHello: sayHello,
17+
getSecretNickname: getSecretNickname
18+
};
19+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace person {
2+
// Public variables
3+
const firstName: string = "Baptiste",
4+
lastName: string = "Vannesson";
5+
6+
// Private variable
7+
const secretNickname: string = "Bada";
8+
9+
// Public functions
10+
const sayHello = (): string => `Hello, ${firstName} ${lastName}!`;
11+
const getSecretNickname = (): string => secretNickname;
12+
13+
// Revealed module
14+
export const me = { firstName, lastName, sayHello, getSecretNickname };
15+
}
16+
17+
import me = person.me;
18+
19+
console.log(me.firstName, me.lastName); // OK
20+
console.log(me.sayHello()); // OK
21+
// console.log(me.secretNickname); Oops! Undefined!
22+
console.log(me.getSecretNickname()); // OK

0 commit comments

Comments
 (0)