创建对象并将其推送到数组并获取“无法读取未定义的 属性 ' '”

Creating and pushing object to an array and getting "cannot read property ' ' of undefined

我有这两个对象构造函数和数组:

//Houses
var houseArray = [];

//Generate street adress
var houseStreets = [..., ..., ...];

var generateAddress = function(){
    var index = round(random(0, houseStreets.length));
    var num = round(random(0, 9999));
    return num + " " + houseStreets[index];
};

// House constructor and prototypes
var House = function(address, x, y) {
    this.address = address;
    this.x = x;
    this.y = y;
};

House.prototype.draw = function() {
    rectMode(CENTER);
    rect(this.x, this.y, 30, 20);
    rectMode(CORNER);
};


//Households

var householdArray = [];

//Generate household names
var householdNames = [..., ..., ...];

var generateName = function(){
    var index = round(random(0, householdNames.length));
    return householdNames[index];
};

//Household constructor and prototypes
var Household = function(name, house) {
    this.name = name;
    this.house = house.address;
    this.x = house.x;
    this.y = house.y;
    this.isHomeOwner = true;
    this.isSelling = false;

};

Household.prototype.draw = function() {
    ellipse(this.x, this.y, 10, 10);
};

Household.prototype.determinMove = function() {
    if(random(0, 100) <= 75){
        this.isSelling = true;
    }
};

在我模拟的初始阶段,我创建了 20 户人家和 20 所房子,这 20 户人家居住在这 20 所房子里:

// Populate initial houses
noFill();
houseArray.push(new House(generateAddress(), 15, 10));
houseArray.push(new House(generateAddress(), 50, 10));
houseArray.push(new House(generateAddress(), 85, 10));
houseArray.push(new House(generateAddress(), 120, 10));
houseArray.push(new House(generateAddress(), 155, 10));
houseArray.push(new House(generateAddress(), 190, 10));
houseArray.push(new House(generateAddress(), 225, 10));
houseArray.push(new House(generateAddress(), 260, 10));
houseArray.push(new House(generateAddress(), 295, 10));
houseArray.push(new House(generateAddress(), 330, 10));
houseArray.push(new House(generateAddress(), 15, 389));
houseArray.push(new House(generateAddress(), 50, 389));
houseArray.push(new House(generateAddress(), 85, 389));
houseArray.push(new House(generateAddress(), 120, 389));
houseArray.push(new House(generateAddress(), 155, 389));        
houseArray.push(new House(generateAddress(), 190, 389));
houseArray.push(new House(generateAddress(), 225, 389));
houseArray.push(new House(generateAddress(), 260, 389));
houseArray.push(new House(generateAddress(), 295, 389));
houseArray.push(new House(generateAddress(), 330, 389));

//Populate initial households
for (var i = 0; i < houseArray.length; i++){
    householdArray.push(new Household(generateName(), houseArray[i]));
}

现在,我正在尝试构建一个将在每个新回合开始时调用的函数,该函数将创建 5 个不在任何地方居住的新家庭(未绑定到 houseArray 中的房子),并且有一个随机的 x(在 10 到 390 之间)和随机的 y(在 50 到 350 之间)位置,我似乎无法让它工作。这是我想出的代码:

var newArrival = function() {
    for (i = 0; i < 5; i++) {
    householdArray.push(new Household(generateName()));
    }
};

但是当我这样做时 print(householdArray[22].name) 我得到一个 "cannot read property 'name' of undefined" 错误。另外我还没有弄清楚如何处理随机的 x 和 y 位置。

问题:我的代码有什么问题?在 householdArray 中创建 5 个新家庭并具有随机 x(10 到 390 之间)和随机 y(50 到 350 之间)位置的函数的好方法是什么?

更新:我认为问题出在 Household 构造函数上。它似乎不能很好地处理 house 参数没有提供参数的情况,我不知道如何解决这个问题。在我的 newArrival 函数中,我没有提供 house 参数,因为没有,新来的人还没有房子。这似乎是一个问题。关于如何解决这个问题的任何想法?

问题确实出在构造函数上。出于某种不完全理解的原因,我不得不告诉构造函数在未提供 house 参数的情况下该怎么做。修复它,一切正常。这是代码:

var Household = function(name, house) {
    if (house === undefined) {
        this.name = name;
        this.house = "HOMELESS";
        this.x = random(10, 390);
        this.y = random(50, 350);
        this.isHomeOwner = false;
        this.isSelling = false;
        this.askingPrice = 0;
    } else {
        this.name = name;
        this.house = house;
        this.x = house.x;
        this.y = house.y;
        this.isHomeOwner = true;
        this.isSelling = false;
        this.askingPrice = 0;
    }
};