2013-08-27 285 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);