javascript - 构造一个结构复杂的Javascript对象?

标签 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 ,就像我们定义变量 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();

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

最佳答案

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

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

当然,没有什么可以阻止您在成本内为自己指定一个更短的名称:

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

关于javascript - 构造一个结构复杂的Javascript对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53657708/

相关文章:

JavaScript;背景颜色更改并使用/相同输入改回

javascript - 在我的网站中使用 bing 翻译器

java - 如何在 Java 中有条件地调用不同的构造函数?

javascript - 检查哪些数组元素具有空值

javascript - 如何从 Angular 中的另一个 Controller 内的 Controller 访问对象

c++ - 无法访问 C++ 中全局变量的构造函数中的静态(非原始)成员

c++ - 为什么我的值没有被我的初始化(参数化)构造函数初始化?

java - Mockito:如何测试调用了构造函数?

c# 命名空间已经包含继承类的定义,那么如何添加构造函数

javascript - 如何在网络浏览器上通过 Live555 服务器的 rtsp 协议(protocol)流式传输视频