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

时间:2018-12-06 18:36:46

标签: javascript constructor

我有一个具有这种结构的对象,可以在代码周围实例化

costs: {
    totalPerYear,
    totalEver,

    perMonth: {
        items: {
            depreciation,
            insurance,
            credit,
            inspection,
            roadTaxes,
            fuel,
            maintenance,
            repairsImprovements,
            parking,
            tolls,
            fines,
            washing
        },
        standingCosts,
        runningCosts,
        total
    },

    perUnitDistance: { 
        runningCosts,
        totalCosts
    }
}

我去过reading about constructorsinstantiation。为了简洁,有没有办法为此对象提供一个构造函数,其中所有变量都设置为undefined,就像我们定义变量{{1}时发生的情况一样}?

我有明显的解决办法

var x;

您使用哪种技术来创建结构复杂的对象?

1 个答案:

答案 0 :(得分:3)

如果只需要一个对象,而不需要它具有特殊的原型,则返回对象而不是构造函数的函数非常简单:

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