title: javascript singleton public method "not a function"
body: <p>I give up.  I'm usually a c# developer, but I need javascript for this particular project.</p>
  
  <p>I have a list that I want to be protected with some setters and getters (and also public methods vs private helper methods).  To do this, I've implemented a singleton pattern following Addy Osmani's Singleton pattern  as described in this post: <a href="http://robdodson.me/javascript-design-patterns-singleton/" rel="nofollow noreferrer">http://robdodson.me/javascript-design-patterns-singleton/</a></p>
  
  <p>However, when I try to access the public methods, I get the error "publicMethod is not a function".  </p>
  
  <p>I have a button hooked up to "addToList" and I just want to print out the message to start with.  </p>
  
  <p>Why can't it see my method?</p>
  
  <pre><code>angular
  .module('bacnetui')
  .controller('bacnetuiController', function($scope, devicesFactory,){
  
     devicesFactory.getDevices().then(function (response){
       $scope.devices = response.data;
     }, function(error) {
      console.log(error);
     });
  
  
     $scope.mySingleton = (function () {
      // Instance stores a reference to the Singleton
      var instance;
  
      function init() {
        // Singleton
        var list = [];
  
        // Private methods and variables
        function indexOfDevice(dev){
         ...
       }
  
       function hasBacnet(dev,obj,prop){
         ....
       }
       function newBacnet(obj,prop){
          ....
       }
  
        return {
  
          // Public methods and variables
          publicMethod: function () {
            console.log( "The public can see me!" );
          },
  
          publicProperty: "I am also public"
        };
      };
  
      return {
        // Get the Singleton instance if one exists
        // or create one if it doesn't
        getInstance: function () {
          if ( !instance ) {
            instance = init();
          }
          return instance;
        }
      };
    })();
  
  
    $scope.addToList = function(device,obj,prop) {
      console.log("found a function: " + $scope.mySingleton.publicMethod());
  
      //$scope.myList.addBacnet(device,obj,prop);
    };
  
    $scope.removeFromList = function(device,obj,prop) {};
  
    $scope.saveToFile = function(){
  
    };
  });
  </code></pre>