不能在数组上使用过滤功能

时间:2017-03-26 12:08:57

标签: javascript arrays filter singleton

我学习如何在javascript中编码。我总是错误:"无法读取属性'过滤'未定义"。我在这做错了什么,为什么?

我必须使用Singleton模式和B类构建一个类,它将成为A类的观察者。 我必须将A类的一些实例作为订阅者(观察者)添加到A中,并在A类中的随机值I大于B类中的随机值P时取消订阅任何实例。

var A = (function()
{

  // Instance stores a reference to the Singleton
  var instance;

  function init() {

// Singleton


var i = 0;
let observers = new Array();
function CheckIfGreaterThanI(observer)
  {
      console.log("CHECKING");
      return observer.getP() > this.getI();
  }

return {

  subscribe: function(observer)
  {
      console.log("DODAJĘ");
      observers.push(observer);
  },

  unsubscribe: function(observerss)
  {
      console.log("USUWAM");
      for(i=0;i<observerss.length;i++)
      {
          var index = this.observers.indexOf(observerss[i])

          if (~index) 
          {
              this.observers.splice(index, 1);
          }
      }

  },

  notify: function()
  {
      for(let observer of observers)
      {
          observer.update();
      }
  },

  getI: function()
  {
      return this.i;
  },

  setI: function(value)
  {
      this.i = value;
      this.notify();


///THAT'S THE PLACE WHERE ERROR RISES
      var observersToUnsubscribe = this.observers.filter(this.CheckIfGreaterThanI);
      this.unsubscribe(observersToUnsubscribe);

  }
};

};

return 
{

// Get the Singleton instance if one exists
// or create one if it doesn't
getInstance: function () {

  if ( !instance ) {
    instance = init();
  }

  return instance;
}

};

})();

function B (name,value,a) //observer
{
    this.Name = name;
    this.P = value;
    this.A = a;     
}

B.prototype = 
{
   constructor:B,
   getName : function()
   {
        return this.Name;
   },


   getP : function()
   {
       return this.P;
   },

   update : function()
   {
       if(A.getInstance().getI()<this.P)
       {
           console.log("OK - " + this.Name);
       }
   }
};

for(i=0;i<10;i++)
{
    var bObject = new B(i,Math.random(),A.getInstance());
    A.getInstance().subscribe(bObject);
}

var ChangeIValue = function()
{
    A.getInstance().setI(Math.random());
}


setTimeout(function run()
{
    ChangeIValue();
    setTimeout(run,1000);
}
, 1000);

1 个答案:

答案 0 :(得分:0)

好的,我单独解决了这个问题,背后有很多错误,所以我为此添加了我的解决方案:

var A = (function()
{

  // Instance stores a reference to the Singleton
  var instance;

  function init() {

  // Singleton


  var i = 0;
  var observers =[];
  function CheckIfGreaterThanI(observer)
  {
      return observer.getP() > i;
  }

  return {

  subscribe: function(observer)
  {
      observers.push(observer);
  },

  unsubscribe: function(observersToUnsubscribe)
  {

       for(let observer of observersToUnsubscribe)
       {
           var index = observers.indexOf(observer);
           if(index!=-1)
           {
               observers.splice(index,1);
           }
       }
  },

  notify: function()
  {
      for(let observer of observers)
      {
          observer.update();
      }
  },

  getI: function()
  {
      return i;
  },




  setI: function(value)
  {
      i = value;
      this.notify();


     var observersToUnsubscribe = observers.filter(CheckIfGreaterThanI);
     this.unsubscribe(observersToUnsubscribe);

     return;

  }
};

};

return {

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

};

})();

function B (name,value,a) //observer
{
    this.Name = name;
    this.P = value;
    this.A = a;     


    this.getName = function()
    {
        return this.Name;
    };


    this.getP = function()
    {
        return this.P;
    };

    this.update = function()
    {
        if(A.getInstance().getI()<this.P)
        {
            console.log("OK - " + this.Name);
        }
    };
};

for(j=0;j<10;j++)
{
    var bObject = new B(j,Math.random(),A.getInstance());
    A.getInstance().subscribe(bObject);
}

var ChangeIValue = function()
{
    A.getInstance().setI(Math.random());
}


setTimeout(function run()
{
    ChangeIValue();
    setTimeout(run,1000);
}
, 1000);