2

我在 javascript 中搜索枚举用法。我在 JavaScript 中找到了一个 stackoverflow 链接 枚举?这是一个好的开始。

此链接显示了一个很好的用途,例如

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

4 回答 4

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

};
于 2012-09-29T18:23:19.347 回答
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"}
};

是我的。然而与此同时,我学到了更多,我现在建议不要使用这种方法。不要使用这种语法!当枚举需要序列化(等等)时,这种方法会出现严重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;

但是,这样您的枚举值将在(反)序列化后继续存在。而在今天的网络中,有 Ajax 请求、REST API 和 JSON 响应,这非常重要,以至于我们应该做出牺牲。

阅读我关于这个主题的博文了解更多详情: Javascript 中的枚举 - Stijn de Witt 的博客

于 2015-06-02T11:16:09.320 回答
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"}
                 }

    };
于 2012-09-29T18:23:53.073 回答
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"}

于 2012-09-29T18:26:12.400 回答