2

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];
Community
  • 1
  • 1
Thomas
  • 31,089
  • 118
  • 335
  • 589

4 Answers4

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

};
jfriend00
  • 580,699
  • 78
  • 809
  • 825
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

Stijn de Witt
  • 31,992
  • 6
  • 67
  • 75
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"}
                 }

    };
Lucas Green
  • 3,773
  • 18
  • 25
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"}

Prasanth
  • 5,019
  • 2
  • 24
  • 57