title: Global Variable Lifespan in Javascript
body: <p>I'm in the process of learning React (and Javascript) and working on a data model is part of my self-imposed tutelage. On my first attempt I created a basic javascript object that would be used with a getter and a setter as follows:</p>
  
  <p><strong>story_store.js</strong></p>
  
  <pre><code>var story = {
    id:"",
    title:""
  }
  
  module.exports = {
      getStory: function() {
          return story
      }
  
      setStory: function(id, title) {
          story.id = id;
          story.title = title;
      }
  }
  </code></pre>
  
  <p>I'd then set the object during a network call and would be able to access (get) it until the application terminated. This version worked as expected.</p>
  
  <p>For the next version I placed my model and  getters/setters in separate files and used an array to store the model objects.</p>
  
  <p><strong>story_model.js</strong></p>
  
  <pre><code>export function Story(id, title, text='') {
      let story = {}
      story.id = id;
      story.title = title;
      story.text = text;
  
      return story
  }
  </code></pre>
  
  <p><strong>story_store.js</strong></p>
  
  <pre><code>var stories = [];
  
  module.exports = {
      getStory: function(id) {
          return stories.find(x =&gt; x.id === id);
      },
      setStories: function(stories) {
          stories.forEach(function(story) {
              stories.push(story);
          });
      }
  }
  </code></pre>
  
  <p>This time the setter worked as expected however when I tried to access the array later on in execution it was empty. I'm guessing the issue has to do with the way I'm creating the objects before I put them in an array but that seems strange to me. Any ideas?</p>