3

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];
asked Sep 29, 2012 at 18:20

4 Answers 4

5

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"}
 }
};
answered Sep 29, 2012 at 18:23
Sign up to request clarification or add additional context in comments.

Comments

1

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

answered Jun 2, 2015 at 11:16

Comments

0

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

answered Sep 29, 2012 at 18:26

1 Comment

Don't use this syntax, it won't survive (de)serialization. See my answer below for details.
0

This:

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

Oughtta be this:

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"}
 }
 };
answered Sep 29, 2012 at 18:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.