Skip to content

Instantly share code, notes, and snippets.

@Risto-Stevcev
Created April 5, 2023 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Risto-Stevcev/4f93e1b5368b05ed32d25040e894f41f to your computer and use it in GitHub Desktop.
Save Risto-Stevcev/4f93e1b5368b05ed32d25040e894f41f to your computer and use it in GitHub Desktop.
Inner modules in javascript
// 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