summaryrefslogtreecommitdiff
path: root/demos/12-manual-code-splitting-a.example.js
blob: 8bb040add2716e83279ee4154ad2ed0b16b8b07e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const COST_OF_ITEM = 15
// We use this immediately invoked anonymous function
// to create a private, non global scope (usually called a module).
// Anything that we want exposed publicly must be included in the
// returned object. This pattern is sometimes called
// "revealing modules"
let CartModule = (function() {
  let moduleScopeVariable = 'super secret'

  // Globally private function, never returned from module
  function sideEffects() {
    console.log('doing side effects')
  }

  // we make this public by adding it to
  // the return object below
  function removeFromCart(item) {
    sideEffects()
    console.log(`${item} removed from cart`)
    return -COST_OF_ITEM
  }
  return {
    // we define this function in the public return object,
    // so it's public by definition
    addToCart: function(item) {
      console.log(`${item} added to cart`)
      return COST_OF_ITEM
    },
    removeFromCart
  }
})()