在javascript中使用枚举

时间:2012-09-29 18:20:21

标签: javascript

我在javascript中搜索Enum用法。我找到了一个stackoverflow链接 Enums in 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];

4 个答案:

答案 0 :(得分:5)

您不在对象定义中使用=。您可以使用:来定义新属性 因此,将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"}
             }

};

答案 1 :(得分:1)

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

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

是我的。然而,与此同时我学到了更多,现在我建议反对这个appoach。 不要使用此语法!当需要序列化枚举(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响应非常重要,这是我们应该做出的牺牲。

阅读有关此主题的博文,了解更多详情: this

答案 2 :(得分:0)

此:

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

    };

应该是这样的:

var MSg = {
        Country: { // <- RIGHT HERE
                    GBR: {name_req:"Name Required",email_req:"Email Required"},
                    FRA: {name_req:"FRA Name Required",email_req:"FRA Email Required"}
                 }

    };

答案 3 :(得分:0)

你应该这样做:

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