Skip to content

Commit 2f60c21

Browse files
author
Badacadabra
committed
Add Module (ES5 + ES6 + CoffeeScript + TypeScript)
1 parent 50ef37b commit 2f60c21

File tree

5 files changed

+126
-0
lines changed

5 files changed

+126
-0
lines changed

misc/Module/CoffeeScript/index.coffee

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
Person = do ->
4+
# Private member
5+
secretNickname = ""
6+
7+
class Person
8+
constructor: (@firstName, @lastName, nickname) ->
9+
secretNickname = nickname
10+
11+
sayHello: ->
12+
"Hello, #{this.firstName} #{this.lastName}!"
13+
14+
getSecretNickname: ->
15+
secretNickname
16+
17+
me = new Person "Baptiste", "Vannesson", "Bada"
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

misc/Module/ECMAScript/ES5/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
var Person = (function () {
4+
var secretNickname;
5+
6+
function Person(firstName, lastName, nickname) {
7+
// Public attributes
8+
this.firstName = firstName;
9+
this.lastName = lastName;
10+
// Private attribute
11+
secretNickname = nickname;
12+
}
13+
14+
// Public methods
15+
Person.prototype.sayHello = function () {
16+
return "Hello, " + this.firstName + " " + this.lastName + "!";
17+
};
18+
19+
Person.prototype.getSecretNickname = function () {
20+
return secretNickname;
21+
};
22+
23+
// Entry point of the module
24+
return Person;
25+
})();
26+
27+
var me = new Person("Baptiste", "Vannesson", "Bada");
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

misc/Module/ECMAScript/ES6/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const Person = (() => {
2+
let secretNickname;
3+
4+
return class Person {
5+
constructor(firstName, lastName, nickname) {
6+
// Public attributes
7+
this.firstName = firstName;
8+
this.lastName = lastName;
9+
// Private attribute
10+
secretNickname = nickname;
11+
}
12+
13+
// Public methods
14+
sayHello() {
15+
return `Hello, ${this.firstName} ${this.lastName}!`
16+
}
17+
18+
getSecretNickname() {
19+
return secretNickname;
20+
}
21+
}
22+
})();
23+
24+
let me = new Person("Baptiste", "Vannesson", "Bada");
25+
26+
console.log(me.firstName, me.lastName); // OK
27+
console.log(me.sayHello()); // OK
28+
console.log(me.secretNickname); // Oops! Undefined!
29+
console.log(me.getSecretNickname()); // OK

misc/Module/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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 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 can be for instance another function declared inside 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, the module pattern is not often used anymore. CommonJS, AMD, or built-in ES6 modules are much better solutions nowadays...

misc/Module/TypeScript/index.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace Mankind {
2+
export interface Human {
3+
sayHello(): string;
4+
getSecretNickname(): string;
5+
}
6+
7+
export class Person implements Human {
8+
public firstName: string;
9+
public lastName: string;
10+
private nickname: string;
11+
12+
constructor(firstName: string, lastName: string, nickname: string) {
13+
this.firstName = firstName;
14+
this.lastName = lastName;
15+
this.nickname = nickname;
16+
}
17+
18+
public sayHello(): string {
19+
return `Hello, ${this.firstName} ${this.lastName}!`
20+
}
21+
22+
public getSecretNickname(): string {
23+
return this.nickname;
24+
}
25+
}
26+
}
27+
28+
import Person = Mankind.Person;
29+
let me = new Person("Baptiste", "Vannesson", "Bada");
30+
31+
console.log(me.firstName, me.lastName); // OK
32+
console.log(me.sayHello()); // OK
33+
// console.log(me.nickname); Oops! Undefined!
34+
console.log(me.getSecretNickname()); // OK

0 commit comments

Comments
 (0)