Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript:是否可以自动(在创建对象时)将对象推送到空数组?_Javascript_Arrays_Object_Multidimensional Array - Fatal编程技术网

javascript:是否可以自动(在创建对象时)将对象推送到空数组?

javascript:是否可以自动(在创建对象时)将对象推送到空数组?,javascript,arrays,object,multidimensional-array,Javascript,Arrays,Object,Multidimensional Array,是否每次创建对象(使用我构建的构造函数)时,都会自动将其追加到空数组中 例如,我有一个客户端构造函数: function client (firstName, lastName, id, code, balance) { this.firstName = firstName; this.lastName = lastName; this.id = id; this.code = code; this.balance = balance; } 还有几个例

是否每次创建对象(使用我构建的构造函数)时,都会自动将其追加到空数组中

例如,我有一个客户端构造函数:

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);
我一直在添加客户端,我不希望每次注册客户端(创建对象)时都需要。将它推送到我的数组中,如下所示:

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

该示例还假设只有一个“singleton”clientsArray。否则你可以把它当作一个论点。我认为第一个版本更干净。

一种方法是让阵列原型本身负责注册客户端:

clientsArray.prototype.registerClient = function(firstName, lastName, id, code, balance){
    this.push(new client(firstName, lastName, id, code, balance))
};
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);
在此之后,您可以执行以下操作:

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

该示例还假设只有一个“singleton”clientsArray。否则你可以把它当作一个论点。但我认为第一个版本更简洁。

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

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);
我认为您现在采用的方法是最好的方法,像这样注入依赖关系

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

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

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

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

您不能将数组传递到构造函数中并在那里进行推送吗?
clientsArray
位于一个可以从
client
访问的范围内?如果没有,只需编写一个包装器函数-
CreateClient
,该函数同时执行
新客户端
推送
…添加
clientsArray.push(此)
进入构造函数创建一个带有内部数组的
ClientManager
并创建一个
ClientManager.Add(ctor args)/ClientManager.get(“fred”)…
你不能在构造函数中传递数组并在那里推送吗?在可以从
客户端访问的范围内有
clientsArray
?如果没有,只需编写一个包装器函数-
CreateClient
,该函数同时执行
新客户端
推送
…添加
clientsArray.push(此)编码到构造函数中或使用内部数组创建
ClientManager
,并创建
ClientManager.Add(ctor args)/ClientManager.get(“fred”)…