简体   繁体   English

javascript singleton公共方法“不是函数”

[英]javascript singleton public method “not a function”

I give up. 我放弃。 I'm usually ac# developer, but I need javascript for this particular project. 我通常是ac#开发人员,但此特定项​​目需要javascript。

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: http://robdodson.me/javascript-design-patterns-singleton/ 为此,我已按照Addy Osmani的Singleton模式实现了单例模式,如本文中所述: http : //robdodson.me/javascript-design-patterns-singleton/

However, when I try to access the public methods, I get the error "publicMethod is not a function". 但是,当我尝试访问公共方法时,出现错误“ publicMethod不是函数”。

I have a button hooked up to "addToList" and I just want to print out the message to start with. 我有一个连接到“ addToList”的按钮,我只想打印出开始的消息。

Why can't it see my method? 为什么看不到我的方法?

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(){

  };
});

You need to use $scope.mySingleton.getInstance().publicMethod() as @Robby pointed out in the comment. 您需要使用$scope.mySingleton.getInstance().publicMethod()在注释中指出的$scope.mySingleton.getInstance().publicMethod()

Following is the flow: 以下是流程:

  $scope.mySingleton = (function () {
    ...
    function init() {
      ...
      return {
        publicMethod: function () {
          ...
        },
      };
    };

    return {
      // Get the Singleton instance if one exists
      // or create one if it doesn't
      getInstance: function () {
        if ( !instance ) {
          instance = init();
        }
        return instance;
      }
    };
  })();

The above structure returns mySingleton with an object assigned to it as: 上面的结构返回mySingleton ,并mySingleton分配了一个对象,如下所示:

{
   getInstance: function() {...}
}

Once you call that then you have access to what init() returned which is the target publicMethod() . 一旦调用它,就可以访问返回的init() ,它是目标publicMethod()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM