javascript构造函数中的构造函数?

时间:2013-08-27 19:21:35

标签: javascript object constructor

我想在构造函数中使用构造函数 - 我已经搜索了stackoverflow并广泛搜索了......

我有一个构造函数RentalProperty

function RentalProperty(numberOfUnits, address, dateAcquired, acCost, isFinanced,                
loanAmount){
this.numberOfUnits = numberOfUnits;
this.address = address;
this.dateAcquired = new Date(dateAcquired);
this.acCost = acCost;
this.isFinanced = isFinanced; 
this.loanAmount = 0;   
this.newValue = 0;
this.equity = function(){
    if (this.newValue === 0){
        return (this.acCost - this.loanAmount);
    } else {
        return (this.newValue - this.loanAmount);
    }
 };
}

RentalProperty的每个实例都会有一系列unit个对象,我希望unit1unit2unit3等等。 RentalProperty的实例只有一个unit,而其他实例可能有六个,十二个或更多。我在这里做的方式似乎不正确,因为有很多重复的代码,我需要制作大量的unit对象,这些对象可能不会用于{{的特定实例1}}:

RentalProperty

我尝试了各种语法组合(我已经把头发拉了几个小时),在RentalProperty.prototype.unit1 = { unitNumber : "1", monthlyRent: 0, leaseStart: new Date(0), leaseEnd: new Date(0), numBeds: 0, isSec8: false, tenantPortion: 0, sec8Portion: 0 }; RentalProperty.prototype.unit2 = { unitNumber : "2", monthlyRent: 0, leaseStart: new Date(0), leaseEnd: new Date(0), numBeds: 0, isSec8: false, tenantPortion: 0, sec8Portion: 0 }; RentalProperty.prototype.unit3 = { unitNumber : "3", monthlyRent: 0, leaseStart: new Date(0), leaseEnd: new Date(0), numBeds: 0, isSec8: false, tenantPortion: 0, sec8Portion: 0 }; 构造函数中放置一个unit构造函数,代码如下:

RentalProperty

....希望这会使用.... this.unit["for(i=0, i < this.numberOfUnits, i++){return i;}"] = { unitNumber : "i", monthlyRent: 0, leaseStart: new Date(0), leaseEnd: new Date(0), numBeds: 0, isSec8: false, tenantPortion: 0, sec8Portion: 0 }; 的{​​{1}}属性的值创建正确的units个数,但它会给我“缺少操作数”。

我也尝试过:

this.numOfUnits

....但是当我尝试使用RentalProperty实例创建一个新的....//'new Object' added this.unit[for(i=0, i < this.numberOfUnits, i++){return i;}] = new Object{ unitNumber : "i", monthlyRent: 0, leaseStart: new Date(0), leaseEnd: new Date(0), numBeds: 0, isSec8: false, tenantPortion: 0, sec8Portion: 0 }; .... ....//trying to make another constructor function Units(){ unitNumber = "1"; monthlyRent = 0; leaseStart = new Date(0); leaseEnd = new Date(0); numBeds = 0; isSec8 = false; tenantPortion = 0; sec8Portion = 0; } var yale = new RentalProperty() var yale.unit33 = new Units(); 类的实例时,它以点表示法形式显示为“意外令牌”。

我只学习了2个月的代码(每个html和javascript约一个月),所以我很确定这是一个noob问题。任何帮助将不胜感激.....这也是我的第一个stackoverflow问题所以如果我的格式化已关闭,请接受我的道歉。

4 个答案:

答案 0 :(得分:1)

好像你想要

this.units = []; // an array which
for (var i=0; i < this.numberOfUnits; i++) { // in a loop
    this.units[i] = { // is filled with objects
        unitNumber : i,
        monthlyRent: 0,
        leaseStart: new Date(0),
        leaseEnd: new Date(0),
        numBeds: 0, 
        isSec8: false,
        tenantPortion: 0,
        sec8Portion: 0
    };
}

当然,您也可以使用new Units(i)或其他东西来代替对象文字来创建单位对象。

如果你不想要一个数组,但是在你的对象上做了数字属性(我不鼓励这样做),那就是

this["unit"+i] = …

答案 1 :(得分:1)

只考虑 Unit

的完全独立的构造函数
RentalProperty.Unit = function Unit(i) {
    if (undefined === i) i = 1;
    this.unitNumber = '' + i;
    this.monthlyRent = 0;
    this.leaseStart = new Date(0);
    this.leaseEnd = new Date(0);
    this.numBeds = 0;
    this.isSec8 = false;
    this.tenantPortion = 0;
    this.sec8Portion = 0;
};

我把它作为RentalProperty的财产,以保持一切整洁 接下来在 RentalProperty 构造函数中生成所有单位,或者然后..

this.units = [];
var i;
for (i = 0; i < this.numberOfUnits; ++i) {
    this.units.push(new RentalProperty.Unit(i));
}

这样做也意味着您可以根据需要为 Unit 设置原型链,并且您可以确认单位u确实是使用u instanceof RentalProperty.Unit单位

答案 2 :(得分:1)

你太复杂了。只要已经定义了Unit构造函数,您就可以这样做:

function RentalProperty(numberOfUnits){
    this.numberOfUnits = numberOfUnits;
    this.units = [];
    for (var i = 0; i < numberOfUnits; ++i) {
       this.units.push(new Unit(i));
    }
}

此构造函数可以是全局范围的,如

function Unit(unitNumber) {
    this.unitNumber = unitNumber;
}

或者您也可以将其设为RentalProperty

的属性
RentalProperty.Unit = function(unitNumber) {
    this.unitNumber = unitNumber;
}

在这种情况下,您将使用new RentalProperty.Unit(i)创建单位。

答案 3 :(得分:0)

问题已经多次回答,但这里是完整的代码。

function Unit(rentProp){
  this.unitNumber = ""+(rentProp.units.length+1);
  this.rentalProperty=rentProp
  //all of the foolowing props could go on prototype
  //because the constructor fills them only with default values
  //later you'll assign new values to them with
  //someUnitInstance.monthlyRent=... 
  this.monthlyRent = 0;
  this.leaseStart = null;
  this.leaseEnd = null;
  this.numBeds = 0;
  this.isSec8 = false;
  this.tenantPortion = 0;
  this.sec8Portion = 0;
}

function RentalProperty(numberOfUnits, address
 , dateAcquired, acCost, isFinanced
 ,loanAmount){
  this.numberOfUnits = numberOfUnits;
  this.address = address;
  this.dateAcquired = new Date(dateAcquired);
  this.acCost = acCost;
  this.isFinanced = isFinanced;
  this.units=[];
};
//default values on prototype, assuming you're not using
//hasOwnProperty later to iterate through properties because
//defaults (on prototype) won't show up
RentalProperty.prototype.loanAmount = 0;   
RentalProperty.prototype.newValue = 0;
RentalProperty.prototype.equity = function(){
  if (this.newValue === 0){
    return (this.acCost - this.loanAmount);
  } else {
    return (this.newValue - this.loanAmount);
  }
};
RentalProperty.prototype.addUnit = function(){
  this.units.push(new Unit(this));
}

var r=new RentalProperty();
r.addUnit();
r.addUnit();
console.log(r.units);