Javascript对象构造函数不会注册参数

时间:2016-08-16 21:58:48

标签: javascript arrays object constructor parameter-passing

为什么isSinglehousehold参数在创建它们时并没有传递给Person对象的相应属性,并且在脚本内部的脚本末尾将它们推送到kidsArrswitch虽然先前在脚本中创建并被推送到s20to24Arr的实例似乎正在正确获取参数?我错过了什么?



startPop = 1000

var getRandom = function(min, max) {
  return Math.random() * (max - min) + min;
};

var lastNames = ['SMITH', 'JOHNSON', 'WILLIAMS', 'JONES', 'BROWN', 'DAVIS', 'MILLER'];
var firstNamesMale = ["Michael", "Christopher", "Jason", "David", "James", "John"];
var firstNamesFemale = ["Jennifer", "Amy", "Melissa", "Michelle", "Kimberly"];

var sex = ["Male", "Female"];

var genSex = function() {
    var index = Math.round(getRandom(0, sex.length - 1));
    return sex[index];
};

var genLastName = function() {
  var index = Math.round(getRandom(0, lastNames.length - 1));
  return lastNames[index];
};

var genFirstNameMale = function() {
  var index = Math.round(getRandom(0, firstNamesMale.length - 1));
  return firstNamesMale[index];
};

var genFirstNameFemale = function() {
  var index = Math.round(getRandom(0, firstNamesFemale.length - 1));
  return firstNamesFemale[index];
};

var s20to24Arr = [];
var householdArray = [];
var kidsArr = [];

var sing20to24 = Math.round(startPop * 0.088 * 0.8385);

var Person = function(sex, age, isSingle, lastName, household) {
  if (sex === undefined) {
    this.sex = genSex();
  } else {
    this.sex = sex;
  }
  if (lastName === undefined) {
    this.lastName = genLastName();
  } else {
    this.lastName = lastName;
  }
  this.firstName = this.sex === "Male" ? genFirstNameMale() : genFirstNameFemale();
  if (age === undefined) {
    this.age = genAgeAdult();
  } else {
    this.age = age;
  }
  //this.income = 0;
  //this.isEmployed = true;
  //this.isRetired = false;
  this.isSingle = isSingle;
  this.household = household;
};

var Household = function(man, woman) {
  this.man = man;
  this.woman = woman;
  //this.house = undefined;
  this.kid1 = undefined;
  this.kid2 = undefined;
  this.kid3 = undefined;
  //this.income : this.man.income + this.woman.income;
  //this.expenses : 0;
  //this.savings : 0;
  //this.debts : 0;
  //this.isHomeOwner : true;
};

var numKids = function(isSingle) {
  var num = getRandom(0, 100);
  if (!isSingle) {
    if (num <= 47.73) {
      return 0;
    } else if (num <= 68.22) {
      return 1;
    } else if (num <= 90.46) {
      return 2;
    } else {
      return 3;
    }
  } else {
    if (num <= 82.31) {
      return 0;
    } else if (num <= 93.04) {
      return 1;
    } else if (num <= 98.11) {
      return 2;
    } else {
      return 3;
    }
  }

};

//This creates instances of the Person object with lastName, firstName, sex, age, isSingle and household properties. It also creates an instance of the Household object.
for (var i = 0; i < sing20to24; i += 2) {
  s20to24Arr.push(new Person("Male", Math.round(getRandom(20, 24)), true));
  s20to24Arr.push(new Person("Female", Math.round(getRandom(20, 24)), true));
  var curMale = s20to24Arr[i],
    curFem = s20to24Arr[i + 1];
  householdArray.push(new Household(curMale));
  var index = householdArray.length > 0 ? householdArray.length - 1 : 0;
  curMale.household = householdArray[index];
  householdArray.push(new Household(undefined, curFem));
  curFem.household = householdArray[index + 1];
  var kids = numKids(true);
  switch (kids) {
    case 0:
      break;
    case 1:
      kidsArr.push(new Person(undefined, Math.round(getRandom(0, 5), true, curFem.lastName, 
        curFem.household)));
      var ind = kidsArr.lenght - 1;
      curFem.household.kid1 = kidsArr[ind];
      break;
    case 2:
      kidsArr.push(new Person(undefined, Math.round(getRandom(0, 5), true, curFem.lastName, 
        curFem.household)));
      kidsArr.push(new Person(undefined, Math.round(getRandom(0, 5), true, curFem.lastName, 
        curFem.household)));
      var ind = kidsArr.lenght - 1;
      curFem.household.kid1 = kidsArr[ind - 1];
      curFem.household.kid2 = kidsArr[ind];
      break;
    case 3:
      kidsArr.push(new Person(undefined, Math.round(getRandom(0, 5), true, curFem.lastName, 
        curFem.household)));
      kidsArr.push(new Person(undefined, Math.round(getRandom(0, 5), true, curFem.lastName, 
        curFem.household)));
      kidsArr.push(new Person(undefined, Math.round(getRandom(0, 5), true, curFem.lastName, 
        curFem.household)));
      var ind = kidsArr.lenght - 1;
      curFem.household.kid1 = kidsArr[ind - 2];
      curFem.household.kid2 = kidsArr[ind - 1];
      curFem.household.kid3 = kidsArr[ind];
      break;
  }
}
console.log(s20to24Arr[0]);
console.log(kidsArr[0]);
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您确定要将括号放在正确的位置吗?

你这样做:

new Person(
    undefined, 
    Math.round(
        getRandom(0, 5),
        true,
        curFem.lastName, 
        curFem.household
    )
)

我故意缩进代码,以便您可以看到错误。您将2个参数传递给Person构造函数,第二个参数是Math.round(...),它会占用其余的参数。

应该是这样的:

new Person(
    undefined,
    Math.round(getRandom(0, 5)),
    true,
    curFem.lastName, 
    curFem.household
)

我没有检查任何其他内容,可能会有更多错误,但这是一个被忽视的基本错误。

s20to24Arr获取正确的对象,因为你正确地放了括号:

s20to24Arr.push(
    new Person("Male", Math.round(getRandom(20, 24)), true) // here Math.round takes only 1 parameter as it should
);