我想我搜索計算器和廣泛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問題,所以請接受我的道歉,如果我的格式是關閉的。
它究竟在哪裏說「意外標記」? – Bergi
Bergi- it在我的IDE結果窗格中顯示「意外的標記」(lol-我是這樣的noob,我不知道這是否正確的術語!) – CoolestUsername
我的意思是「*什麼行,什麼代碼?*」不是「 *在哪個屏幕上*「:-) – Bergi