2013-08-27 88 views
0

我想我搜索计算器和广泛GOOGLE了constructor-内的构造......构造函数在JavaScript中的构造?

我有一个构造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,而其他实例可能有六个,十二个或更多。我在这里做的方式似乎不正确的,因为很多的代码重复,我将需要大量的可能不被用于RentalProperty特定实例unit对象:

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 
    }; 

....希望这会使用的值创建正确的数字财产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大约一个月),所以我很确定这是一个noob问题。任何帮助将非常感谢.....这也是我的第一个stackoverflow问题,所以请接受我的道歉,如果我的格式是关闭的。

+0

它究竟在哪里说“意外标记”? – Bergi

+0

Bergi- it在我的IDE结果窗格中显示“意外的标记”(lol-我是这样的noob,我不知道这是否正确的术语!) – CoolestUsername

+0

我的意思是“*什么行,什么代码?*”不是“ *在哪个屏幕上*“:-) – Bergi

回答

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

试想一个完全独立的构造函数单位

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)); 
} 

这样做,这样也意味着你可以建立一个原型单位,如果你希望,你可以确认一个单元u确实使用u instanceof RentalProperty.Unit一个单位

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)

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);