I have an Object with this structure that I instantiate all around the code
costs: {
totalPerYear,
totalEver,
perMonth: {
items: {
depreciation,
insurance,
credit,
inspection,
roadTaxes,
fuel,
maintenance,
repairsImprovements,
parking,
tolls,
fines,
washing
},
standingCosts,
runningCosts,
total
},
perUnitDistance: {
runningCosts,
totalCosts
}
}
I've been reading about constructors and instantiation. Is there a way, for the sake of conciseness, to have a constructor for this object wherein all the variables are set to undefined, like what happens when we define a variable var x;?
I have the obvious solution
function Costs(){
this.totalPerYear = undefined;
this.totalEver = undefined;
this.perMonth = {
items: {
depreciation: undefined,
insurance: undefined,
credit: undefined,
inspection: undefined,
roadTaxes: undefined,
fuel: undefined,
maintenance: undefined,
repairsImprovements: undefined,
parking: undefined,
tolls: undefined,
fines: undefined,
washing: undefined
},
standingCosts: undefined,
runningCosts: undefined,
total: undefined
};
this.perUnitDistance = {
runningCosts: undefined,
totalCosts: undefined
};
};
var userCosts = new Costs();
Which techniques do you use to create an object with a complex structure?
this.totalPerYear: undefinedis not valid syntax.:should be=.