简体   繁体   English

构造具有复杂结构的Javascript对象?

[英]Construct a Javascript object with a complex structure?

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; 为了简洁起见 ,有没有办法为此对象提供一个构造函数,其中所有变量都设置为undefined ,就像定义变量var x;时发生的情况一样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? 您使用哪些技术来创建具有复杂结构的对象?

If you just want an object and don't need it to have a special prototype, a function returning an object rather than a constructor is quite straightforward: 如果只需要一个对象,而不需要它具有特殊的原型,则返回对象而不是构造函数的函数非常简单:

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
            }
        }
    };
}

Example: 例:

 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; } 

There is, of course, nothing to prevent your giving yourself a shorter name to use within costs : 当然,没有什么可以阻止您在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
            }
        }
    };
}

Example: 例:

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM