[英]Construct a Javascript object with a complex structure?
我有一個具有這種結構的對象,可以在代碼周圍實例化所有對象
costs: {
totalPerYear,
totalEver,
perMonth: {
items: {
depreciation,
insurance,
credit,
inspection,
roadTaxes,
fuel,
maintenance,
repairsImprovements,
parking,
tolls,
fines,
washing
},
standingCosts,
runningCosts,
total
},
perUnitDistance: {
runningCosts,
totalCosts
}
}
我一直在閱讀有關構造函數和實例化的內容 。 為了簡潔起見 ,有沒有辦法為此對象提供一個構造函數,其中所有變量都設置為undefined
,就像定義變量var x;
時發生的情況一樣var x;
?
我有明顯的解決方案
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();
您使用哪些技術來創建具有復雜結構的對象?
如果只需要一個對象,而不需要它具有特殊的原型,則返回對象而不是構造函數的函數非常簡單:
function costs() {
return {
costs: {
totalPerYear: undefined,
totalEver: undefined,
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
},
perUnitDistance: {
runningCosts: undefined,
totalCosts: undefined
}
}
};
}
例:
function costs() { return { costs: { totalPerYear: undefined, totalEver: undefined, 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 }, perUnitDistance: { runningCosts: undefined, totalCosts: undefined } } }; } console.log(costs());
.as-console-wrapper { max-height: 100% !important; }
當然,沒有什么可以阻止您在costs
范圍內給自己起一個簡短的名字:
function costs() {
const u = undefined;
return {
costs: {
totalPerYear: u,
totalEver: u,
perMonth: {
items: {
depreciation: u,
insurance: u,
credit: u,
inspection: u,
roadTaxes: u,
fuel: u,
maintenance: u,
repairsImprovements: u,
parking: u,
tolls: u,
fines: u,
washing: u
},
standingCosts: u,
runningCosts: u,
total: u
},
perUnitDistance: {
runningCosts: u,
totalCosts: u
}
}
};
}
例:
function costs() { const u = undefined; return { costs: { totalPerYear: u, totalEver: u, perMonth: { items: { depreciation: u, insurance: u, credit: u, inspection: u, roadTaxes: u, fuel: u, maintenance: u, repairsImprovements: u, parking: u, tolls: u, fines: u, washing: u }, standingCosts: u, runningCosts: u, total: u }, perUnitDistance: { runningCosts: u, totalCosts: u } } }; } console.log(costs());
.as-console-wrapper { max-height: 100% !important; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.