I want a constructor within a constructor- I've searched stackoverflow and googled extensively......
I have a constructor 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);
}
};
}
Each instance of RentalProperty
will have a series of unit
objects that I would like to be unit1
, unit2
, unit3
, etc. However some instances of RentalProperty
will have only one unit
while others may have six, twelve, or more. The way I'm doing it here doesn't seem right as there is a lot of repetition of code and I will need to make a large number of unit
objects that may not be used for a particular instance of 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
};
I tried various combinations of syntax (I've been pulling my hair out for hours) to put a unit
constructor within the RentalProperty
constructor with code such as:
....
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
};
....hoping that this would create the correct number of units
using the value of the this.numOfUnits
property of RentalProperty
but it gives me "missing operand".
I have also tried:
....//'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();
....but when I try to make an instance of the new Units
class with the RentalProperty
instance before it in dot notation it says 'Unexpected Token'.
I have only been learning to code for 2 months (approx one month each of html and javascript) so I'm pretty sure this is a noob question. Any help would be much appreciated..... This is also my first stackoverflow question so please accept my apologies if my formatting is off.