创建对象并将其推送到数组并获取未定义

时间:2016-08-01 16:04:12

标签: javascript arrays oop object 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)时,我得到一个“无法读取属性”名称的“未定义”错误。此外,我还没有想出如何处理随机的x和y位置。

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

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

1 个答案:

答案 0 :(得分:0)

问题确实在于构造函数。由于某些原因,我不得不完全理解,我必须告诉构造函数在没有输入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;
    }
};