title: Reference an event listener across JavaScript objects
body: <p>I have written an API that would allow different applications to handle online/offline detection:</p>
  <pre><code>function onOffAPI() {
  
      var connection;
  
      function init() {
          var onLoadStatus = window.navigator.onLine;
  
          window.addEventListener('online', function() {
              connection.status = true;
          }, false);
  
          window.addEventListener('offline', function() {
              connection.status = false;
          }, false);
  
          return {
              status: onLoadStatus
          };
      }
  
      return {
          getStatus: function () {
              if (!connection) {
                  connection = init();
              }
              return connection;
          }
      };
  }
  </code></pre>
  <p>So a sample app uses the following code to detect whenever the application is online or offline:</p>
  <pre><code>var $alert = $('.alert');
  var notification = new onOffAPI();
  var networkStatus = notification.getStatus().status;
  
  $alert.text('Online: ' + notification.getStatus().status); //returns true if online, false if offline
  </code></pre>
  <p>Being able to reference the API when the application loads is straightforward, but I'm perplexed as how to reference the event listeners whenever the network connection were to suddenly change from online to offline, or vice versa.  Could someone help me out here?</p>