在JavaScript中使用枚举

我在JavaScript中搜索Enum用法。 我发现一个stackoverflow链接在JavaScript中的枚举? 这是一个好的开始。

这个链接显示了一个很好的用法

var SIZE = {
SMALL : {value: 0, name: "Small", code: "S"}, 
MEDIUM: {value: 1, name: "Medium", code: "M"}, 
LARGE : {value: 2, name: "Large", code: "L"}
};

var currentSize = SIZE.MEDIUM;
if (currentSize == SIZE.MEDIUM) {
// this alerts: "1: Medium"
alert(currentSize.value + ": " + currentSize.name);
}

我的要求有点不同,为什么我改变了上面的代码

var MSg = {
        Country= {
                    GBR: {name_req:"Name Required",email_req:"Email Required"},
                    FRA: {name_req:"FRA Name Required",email_req:"FRA Email Required"}
                 }

    };

but it is giving error. so please guide me how to write the above code without error. also tell me the enum code will work in all the browser? thanks.

为我自己的未来参考

var dialog =
    {
        MainContainer:
        {
            Country:
            {
                GBR:
                    {
                        height: 365, width: 257
                    },
                FRA:
                    {
                        height: 375, width: 310
                    }

            }
        },
        SubContainer:
        {
            Country:
            {
                GBR:
                    {
                        height: 0, width: 257
                    },
                FRA:
                    {
                        height: 0, width: 300
                    }
            }
        }
    };

    var Validation =
    {
        Msg:
        {
            Country:
            {
                GBR:
                    {
                        Name: "Name Required",
                        Subject: "Subject Required",
                        Email: "Email Required",
                        Invalid_Email: "Invalid Email Address",
                        Feedback: "Feedback details Required",
                        Success: "Feedback send successfully",
                        Fail: "Feedback send fail"
                    },
                USA:
                    {
                        Name: "Name Required",
                        Subject: "Subject Required",
                        Email: "Email Required",
                        Invalid_Email: "Invalid Email Address",
                        Feedback: "Feedback details Required",
                        Success: "Feedback send successfully",
                        Fail: "Feedback send fail"
                    }
            }
        }
    };

转让或设置国家

 var feedCookie = getCookie('SetCountry');
 feedCookie = (feedCookie == '' ? 'GBR' : feedCookie);
 var dialog_Main = dialog.MainContainer.Country[feedCookie];
 var dialog_Sub = dialog.SubContainer.Country[feedCookie];

你应该做:

var MSg = {
    Country : {
                GBR: {name_req:"Name Required",email_req:"Email Required"},
                FRA: {name_req:"FRA Name Required",email_req:"FRA Email Required"}
             }

};

现在, var c =MSg.Country.GBR会给你c = {name_req:"Name Required",email_req:"Email Required"}


你不要在对象定义中使用= 。 您使用:定义一个新的属性。
所以改变Country = { Country: {像这样:

var MSg = {
    Country: {
                GBR: {name_req:"Name Required",email_req:"Email Required"},
                FRA: {name_req:"FRA Name Required",email_req:"FRA Email Required"}
             }

};

来自OP引用的问题的原始答案,提示了这种语法:

var SIZE = {
    SMALL : {value: 0, name: "Small", code: "S"}, 
    MEDIUM: {value: 1, name: "Medium", code: "M"}, 
    LARGE : {value: 2, name: "Large", code: "L"}
};

是我的。 然而在此期间,我学到了更多,现在我建议不要这样做。 不要使用这种语法! 当枚举需要序列化时( JSON.stringify()JSON.parse()等),这种方法存在严重问题。

改用此语法:

var SIZE = {
    SMALL: 0,
    MEDIUM: 1,
    LARGE: 2,

    properties: {
        0: {value: 0, name: "Small", code: "S"},
        1: {value: 1, name: "Medium", code: "M"}, 
        2: {value: 2, name: "Large", code: "L"}
    }
};

有点冗长,是的。 访问枚举值的属性也有点不直观:

var size = SIZE.SMALL;
var code = SIZE.properties[size].code;

但是,这样你的枚举值将会存活(de)序列化。 在今天的网络中,它具有Ajax请求,REST API和JSON响应,这非常重要,所以我们应该做出牺牲。

阅读我关于此主题的博客文章以获取更多详细信息:Javascript中的枚举 - Stijn de Witt的博客

链接地址: http://www.djcxy.com/p/91773.html

上一篇: Enum usage in javascript

下一篇: JavaScript, defining unmodifiable Enumerations