0

我想要一个构造函数中的构造函数-我已经搜索了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我想成为的对象unit1, unit2,unit3等。然而,有些实例RentalProperty只有一个unit,而其他实例可能有六个、十二个或更多。我在这里做的方式似乎不正确,因为有很多重复的代码,我需要制作大量unit可能不会用于特定实例的对象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
    };

....希望这会创建正确数量的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(); 

....但是当我尝试使用它之前的实例以点表示法创建新Units类的实例时,它会显示“意外令牌”。RentalProperty

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

4

4 回答 4

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设置原型链,并且可以确认一个单元确实是一个使用.uu instanceof RentalProperty.Unit

于 2013-08-27T19:25:22.990 回答
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).

于 2013-08-27T19:25:41.053 回答
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] = …
于 2013-08-27T19:25:13.143 回答
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);
于 2013-08-28T00:51:53.310 回答