Created
April 5, 2023 00:54
-
-
Save Risto-Stevcev/4f93e1b5368b05ed32d25040e894f41f to your computer and use it in GitHub Desktop.
Inner modules in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using let and a block statement allows you to create Ocaml-like inner modules | |
let Foo | |
{ | |
// This is in a closure, but the module itself is declarative -- it's not a class | |
let norf = 567 | |
// You can nest inner modules too | |
let Bar | |
{ | |
let exclaim = '!' | |
Bar = name => ({ greet: `${name}${exclaim}` }) | |
} | |
// But functors are most easily expressed as just regular functions with closure | |
const Bar2 = name => { | |
let exclaim = '!' | |
let greet = `${name}${exclaim}` | |
return { greet } | |
} | |
let bar = baz => baz + 123 | |
let qux = 456 + norf | |
Foo = { bar, qux, r: Bar('risto'), r2: Bar2('risto') } | |
} | |
console.log(Foo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment