【问题标题】:javascript: is it possible to push objects to an empty array automatically (when they are created)?javascript:是否可以自动将对象推送到空数组(当它们被创建时)?
【发布时间】:2015-09-22 13:13:56
【问题描述】:

是否有可能每次创建一个对象(使用我构建的构造函数)时,它都会自动附加到一个空数组?

例如,我有一个 client 构造函数:

function client (firstName, lastName, id, code, balance) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.id = id;
    this.code = code;
    this.balance = balance;
} 

还有几个例子:

var fred = new client("fred", "sou", 123456, 4545, 2500);
var george = new client("george", "potter", 852564, 5858, 1000);
var will = new client("will", "smith", 475896, 1234, 45000);

我一直在添加客户端,我不希望每次注册客户端(创建对象)时都需要 .push 将其添加到我的数组,像这样:

var fred = new client("fred", "sou", 123456, 4545, 2500);
clientsArray.push(fred);
var george = new client("george", "potter", 852564, 5858, 1000);
clientsArray.push(george);
var will = new client("will", "smith", 475896, 1234, 45000);
clientsArray.push(will);

那么,有没有办法自动定义它?我认为这可能与客户端构造函数有关,但它不是一个普通的方法(因为再次,我必须为我创建的每个对象调用此方法)。

有人有想法吗?

【问题讨论】:

  • 你不能在构造函数中传递数组并在那里推送吗?
  • clientsArray 是否在可以从client 访问的范围内?如果没有,只需编写一个包装函数 - CreateClient,它同时执行 new clientpush...
  • 在构造函数中添加clientsArray.push(this);
  • 使用内部数组创建ClientManager 并创建clientManager.Add( ctor args ) / clientManager.get("fred") ...

标签: javascript arrays object multidimensional-array


【解决方案1】:

这样做的一种方法是让数组原型本身负责注册客户端:

clientsArray.prototype.registerClient = function(firstName, lastName, id, code, balance){
    this.push(new client(firstName, lastName, id, code, balance))
};

在此之后,您可以这样做:

clientsArray.registerClient("fred", "sou", 123456, 4545, 2500);

另一种方法是让客户端将自己注册到数组中(考虑责任 - 我认为这似乎不太正确)。在现实生活中,客户/患者会直接在医生数据库中注册自己,还是医生(的助理)负责注册新客户?很可能是后者(也许他们必须在以后添加患者之前进行一些检查)。无论如何,对于这种方法,只需添加:

function client (firstName, lastName, id, code, balance) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.id = id;
    this.code = code;
    this.balance = balance;

    clientsArray.push(this);
} 

该示例还假设只有一个“单例”clientsArray。否则,您可以将其作为参数传递。不过我认为第一个版本更干净。

【讨论】:

    【解决方案2】:
    var clientsArray = [];
    
    function client (firstName, lastName, id, code, balance) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.id = id;
        this.code = code;
        this.balance = balance;
        clientsArray.push(this);
    } 
    
    var fred = new client("fred", "sou", 123456, 4545, 2500);
    var george = new client("george", "potter", 852564, 5858, 1000);
    var will = new client("will", "smith", 475896, 1234, 45000);
    

    【讨论】:

      【解决方案3】:

      就个人而言,我认为让另一种方法包装对象的构造是错误的方法,更糟糕的是将其绑定到闭包/全局变量。如果您只需要客户端对象而不是推入数组怎么办?或者如果你的构造函数参数改变了怎么办?您必须修改包装构造函数的方法和构造函数以使其适合。

      我认为您现在采用的方法是最好的方法,像这样注入依赖项。

      clientsArray.push(new client("will", "smith", 475896, 1234, 45000));
      

      但如果你真的需要,你可以添加一个可选参数:

      function client (firstName, lastName, id, code, balance, clientsArr) {
          this.firstName = firstName;
          this.lastName = lastName;
          this.id = id;
          this.code = code;
          this.balance = balance;
          if(clientsArr) {
             clientsArr.push(this);
          }
      }
      

      然后:

      var will = new client('will', 'smith', 475896, 1234, 45000, clientsArray);
      

      如果不需要压入数组:

      var will = new client('will', 'smith', 475896, 1234, 45000);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-23
        • 2020-10-24
        • 1970-01-01
        • 2016-12-26
        • 2021-11-13
        • 1970-01-01
        • 1970-01-01
        • 2022-01-26
        相关资源
        最近更新 更多