简体   繁体   English

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

[英]javascript: is it possible to push objects to an empty array automatically (when they are created)?

is it possible that everytime an object is created (with a constructor i've built) it will be appended automatically to an empty array?是否有可能每次创建一个对象(使用我构建的构造函数)时,它都会自动附加到一个空数组中?

for example, i have a client constructor:例如,我有一个客户端构造函数:

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

and several instances:和几个实例:

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

i am adding clients all the time, i don't want that everytime i register a client(creating an object) i would need to .push it to my array, like this:我一直在添加客户端,我不希望每次注册客户端(创建对象)时都需要将其送到我的数组,如下所示:

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

so, is there a way to define it automatically?那么,有没有办法自动定义它? i think that it is probably has something to do with the client constructor, but it's not an ordinary method (because again, i would have to call this method for every object i create).我认为它可能与客户端构造函数有关,但它不是一个普通的方法(因为再次,我必须为我创建的每个对象调用这个方法)。

anyone has an idea?有人有想法吗?

One way of doing this could be making the array prototype itself responsible for registering clients:一种方法是让数组原型本身负责注册客户端:

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

After this, you could do:在此之后,您可以执行以下操作:

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

The other way around would be making the client register itself into the array (consider the responsibility - I don't think that that seems quite right).另一种方法是让客户端将自己注册到数组中(考虑责任 - 我认为这似乎不太正确)。 In real life, would a Client/patient register itself directly in the doctors database, or would the doctor(s assistant) be responsible for registering new clients?在现实生活中,客户/患者是直接在医生数据库中注册自己,还是医生助理负责注册新客户? Most likely the latter (maybe they have to do some checks before the patient is added later on).最有可能是后者(也许他们必须在稍后添加患者之前进行一些检查)。 Anyway, for that approach, just add:无论如何,对于这种方法,只需添加:

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

The example also assumes that there's only one "singleton" clientsArray.该示例还假设只有一个“单例”clientsArray。 Else you could pass it as an argument.否则,您可以将其作为参数传递。 I think the first version is cleaner though.我认为第一个版本更干净。

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

Personally, I think it'd be the wrong approach to let another method wrap the construction of the object and even worse to bind it to a closure/global variable.就我个人而言,我认为让另一种方法包装对象的构造是错误的方法,更糟糕的是将其绑定到闭包/全局变量。 What if you need just the client object and not to push into an array?如果您只需要客户端对象而不需要推送到数组中怎么办? Or what if your constructor parameters change?或者如果你的构造函数参数改变了怎么办? You'd have to modify both the method that wraps the constructor and the constructor to make it fit.您必须修改包装构造函数和构造函数的方法以使其适合。

I think the approach you are following right now is the best approach, to inject the dependency like so.我认为你现在遵循的方法是最好的方法,像这样注入依赖。

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

But if you really must, you could just add an optional parameter:但如果你真的必须,你可以添加一个可选参数:

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

And then:进而:

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

If you don't need to push into an array:如果您不需要推入数组:

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

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

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