我想要一个构造函数中的构造函数-我已经搜索了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问题,所以如果我的格式关闭,请接受我的道歉。