Module examples
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
/* @flow */
|
||||
|
||||
/*
|
||||
In the following code, the only thing that needs
|
||||
an explicit type declaration for Flow, is "name".
|
||||
Flow can work out on its own the rest of the types.
|
||||
*/
|
||||
|
||||
const myCounter = ((name: string) => {
|
||||
let count = 0;
|
||||
|
||||
const get = () => count; // private
|
||||
|
||||
const inc = () => ++count;
|
||||
|
||||
const toString = () => `${name}: ${get()}`;
|
||||
|
||||
return {
|
||||
inc,
|
||||
toString
|
||||
};
|
||||
})("Clicks");
|
||||
|
||||
console.log(myCounter); // an object with "inc" and "toString" properties
|
||||
|
||||
myCounter.inc(); // 1
|
||||
myCounter.inc(); // 2
|
||||
myCounter.inc(); // 3
|
||||
|
||||
myCounter.toString(); // "Clicks: 3"
|
||||
@@ -0,0 +1,19 @@
|
||||
/* @flow */
|
||||
|
||||
let name: string = "";
|
||||
let count: number = 0;
|
||||
|
||||
let get = () => count;
|
||||
let inc = () => ++count;
|
||||
let toString = () => `${name}: ${get()}`;
|
||||
|
||||
/*
|
||||
Since we cannot initialize anything otherwise,
|
||||
a common pattern is to provide a "init()" function
|
||||
to do all necessary initializations.
|
||||
*/
|
||||
const init = (n: string) => {
|
||||
name = n;
|
||||
};
|
||||
|
||||
export default { inc, toString, init }; // everything else is private
|
||||
@@ -0,0 +1,28 @@
|
||||
/* @flow */
|
||||
|
||||
let name: string = "";
|
||||
let count: number = 0;
|
||||
|
||||
let get = () => count;
|
||||
|
||||
let throwNotInit = () => {
|
||||
throw new Error("Not initialized");
|
||||
};
|
||||
|
||||
let inc = throwNotInit;
|
||||
let toString = throwNotInit;
|
||||
|
||||
/*
|
||||
Since we cannot initialize anything otherwise,
|
||||
a common pattern is to provide a "init()" function
|
||||
to do all necessary initializations. In this case,
|
||||
"inc()" and "toString()" won't work as expected
|
||||
if the module wasn't initialized.
|
||||
*/
|
||||
const init = (n: string) => {
|
||||
name = n;
|
||||
inc = () => ++count;
|
||||
toString = () => `${name}: ${get()}`;
|
||||
};
|
||||
|
||||
export default { inc, toString, init }; // everything else is private
|
||||
@@ -0,0 +1,16 @@
|
||||
/* @flow */
|
||||
|
||||
import myCounter from "/home/fkereki/MODERNJS/chapter02/src/module_counter.SIMPLE.js";
|
||||
|
||||
/*
|
||||
Initialize the counter appropriately
|
||||
*/
|
||||
myCounter.init("Clicks");
|
||||
|
||||
/*
|
||||
The rest would work as before
|
||||
*/
|
||||
myCounter.inc(); // 1
|
||||
myCounter.inc(); // 2
|
||||
myCounter.inc(); // 3
|
||||
myCounter.toString(); // "Clicks: 3"
|
||||
@@ -0,0 +1,15 @@
|
||||
// No Flow anywhere!
|
||||
|
||||
let name = "";
|
||||
let count = 0;
|
||||
|
||||
let get = () => count;
|
||||
|
||||
let inc = () => ++count;
|
||||
let toString = () => `${name}: ${get()}`;
|
||||
|
||||
const init = n => {
|
||||
name = n;
|
||||
};
|
||||
|
||||
export default { inc, toString, init }; // everything else is private
|
||||
@@ -0,0 +1,20 @@
|
||||
<http>
|
||||
|
||||
<head> </head>
|
||||
|
||||
<body>
|
||||
See the console for output.
|
||||
|
||||
<script type="module" src="./module_counter.SIMPLE.js"></script>
|
||||
<script type="module">
|
||||
import myCounter from "./module_counter.SIMPLE.js";
|
||||
|
||||
myCounter.init("Clicks");
|
||||
|
||||
console.log(myCounter.inc()); // 1
|
||||
console.log(myCounter.inc()); // 2
|
||||
console.log(myCounter.inc()); // 3
|
||||
console.log(myCounter.toString()); // "Clicks: 3"
|
||||
</script>
|
||||
</body>
|
||||
</http>
|
||||
Reference in New Issue
Block a user