title: CommonJS module pattern
body: <p>I have taken this from </p>
  
  <p><a href="https://facebook.github.io/flux/docs/todo-list.html#content" rel="nofollow">Flux architecture</a></p>
  
  <pre><code>var AppDispatcher = require('../dispatcher/AppDispatcher');
  var EventEmitter = require('events').EventEmitter;
  var TodoConstants = require('../constants/TodoConstants');
  var assign = require('object-assign');
  
  var CHANGE_EVENT = 'change';
  
  var _todos = {}; // collection of todo items
  
  /**
   * Create a TODO item.
   * @param {string} text The content of the TODO
   */
  function create(text) {
    // Using the current timestamp in place of a real id.
    var id = Date.now();
    _todos[id] = {
      id: id,
      complete: false,
      text: text
    };
  }
  
  /**
   * Delete a TODO item.
   * @param {string} id
   */
  function destroy(id) {
    delete _todos[id];
  }
  
  var TodoStore = assign({}, EventEmitter.prototype, {
  
    /**
     * Get the entire collection of TODOs.
     * @return {object}
     */
    getAll: function() {
      return _todos;
    },
  
    emitChange: function() {
      this.emit(CHANGE_EVENT);
    },
  
    /**
     * @param {function} callback
     */
    addChangeListener: function(callback) {
      this.on(CHANGE_EVENT, callback);
    },
  
    /**
     * @param {function} callback
     */
    removeChangeListener: function(callback) {
      this.removeListener(CHANGE_EVENT, callback);
    },
  
    dispatcherIndex: AppDispatcher.register(function(payload) {
      var action = payload.action;
      var text;
  
      switch(action.actionType) {
        case TodoConstants.TODO_CREATE:
          text = action.text.trim();
          if (text !== '') {
            create(text);
            TodoStore.emitChange();
          }
          break;
  
        case TodoConstants.TODO_DESTROY:
          destroy(action.id);
          TodoStore.emitChange();
          break;
  
        // add more cases for other actionTypes, like TODO_UPDATE, etc.
      }
  
      return true; // No errors. Needed by promise in Dispatcher.
    })
  
  });
  </code></pre>
  
  <p>where it says </p>
  
  <blockquote>
    <p>There are a few important things to note in the above code. To start, we are maintaining a private data structure called _todos. This object contains all the individual to-do items. <strong>Because this variable lives outside the class, but within the closure of the module, it remains private</strong> — it cannot be directly changed from outside of the module. This helps us preserve a distinct input/output interface for the flow of data by making it impossible to update the store without using an action.</p>
  </blockquote>
  
  <p>The bold part is unclear to me. <strong>How can js interpreter know that all those code is inside a module closure and not in the globals scope?
  From where the module closure start and where ends?</strong></p>
  
  <p>As far as I know</p>
  
  <blockquote>
    <p>The scope of a variable declared with var is its current execution context, which is either the enclosing function or, <strong>for variables declared outside any function, global</strong>.</p>
  </blockquote>
  
  <p>Any explanation?</p>