在javascript中使用枚举

Tho*_*mas 2 javascript

我在javascript中搜索Enum用法.我在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);
}
Run Code Online (Sandbox Code Playgroud)

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

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.
Run Code Online (Sandbox Code Playgroud)

对于我自己未来的参考

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"
                    }
            }
        }
    };
Run Code Online (Sandbox Code Playgroud)

转让或设定国家

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

jfr*_*d00 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"}
             }

};
Run Code Online (Sandbox Code Playgroud)