Enum usage in javascript
i was searching Enum usage in javascript. i found one stackoverflow link Enums in JavaScript? it is good start.
this link show one good use like
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);
}
my requirement was bit different and that why i change the above code like
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.
For my own future references
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"
}
}
}
};
assignment or set country
var feedCookie = getCookie('SetCountry');
feedCookie = (feedCookie == '' ? 'GBR' : feedCookie);
var dialog_Main = dialog.MainContainer.Country[feedCookie];
var dialog_Sub = dialog.SubContainer.Country[feedCookie];
You should do:
var MSg = {
Country : {
GBR: {name_req:"Name Required",email_req:"Email Required"},
FRA: {name_req:"FRA Name Required",email_req:"FRA Email Required"}
}
};
Now, var c =MSg.Country.GBR
will give you c = {name_req:"Name Required",email_req:"Email Required"}
You don't use =
inside an object definition. You use :
to define a new property.
So change Country = {
to Country: {
like this:
var MSg = {
Country: {
GBR: {name_req:"Name Required",email_req:"Email Required"},
FRA: {name_req:"FRA Name Required",email_req:"FRA Email Required"}
}
};
The original answer from the question referenced by the OP, suggesting this syntax:
var SIZE = {
SMALL : {value: 0, name: "Small", code: "S"},
MEDIUM: {value: 1, name: "Medium", code: "M"},
LARGE : {value: 2, name: "Large", code: "L"}
};
was mine. However in the meanwhile I learned more and I now recommend against this appoach. Don't use this syntax! This approach has serious issues when the enum needs to be serialized ( JSON.stringify()
and JSON.parse()
etc).
Use this syntax instead:
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"}
}
};
A bit more verbose, yes. And accessing the properties of the enum values is also a little less intuitive:
var size = SIZE.SMALL;
var code = SIZE.properties[size].code;
However, this way your enum values will survive (de)serialization. And in today's web with it's Ajax requests, REST API's and JSON responses, that's so important that it's a sacrifice we should make.
Read my blog post about this topic for more details: Enums in Javascript - Stijn de Witt's Blog
链接地址: http://www.djcxy.com/p/91774.html上一篇: EXTJS 3 API转换日期时间
下一篇: 在JavaScript中使用枚举