【问题标题】:can't use filter function on array不能在数组上使用过滤器功能
【发布时间】:2017-03-26 12:08:56
【问题描述】:

我学习如何用 javascript 编写代码。我总是错误:“无法读取未定义的属性'过滤器'”。我在这里做错了什么,为什么?

我必须用单例模式和 B 类构建 A 类,它将成为 A 类的观察者。 我必须将 B 类的一些实例作为订阅者(观察者)添加到 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);

【问题讨论】:

  • 一切都在这里。过滤器除外。
  • @SagarV 它在“setI”方法中
  • 您认为this.observers 是在哪里定义的?该属性不是在 this 上的任何地方创建的。
  • 好的,我已经“让观察者”了,所以我应该改变什么?

标签: javascript arrays filter singleton


【解决方案1】:

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

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-07
    • 2020-05-13
    • 2020-07-10
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 2021-07-15
    相关资源
    最近更新 更多