Implement enumeration in JavaScript using JSON

Source: Internet
Author: User

We will define the Week enumeration as follows:

if(typeof WeekDay == "undefined"){

var WeekDay = {};

WeekDay.Sunday = 0;

WeekDay.Monday = 1;

WeekDay.Tuesday = 2;

WeekDay.Wedesay = 3;

WeekDay.Thursday = 4;

WeekDay.Friday = 5;

WeekDay.Saturday = 6;

}

The test is as follows:

alert(WeekDay.Monday);  // -----> Output: 1

Of course, we have a more intuitive approach. Taking defining the DOM document node type as an example, the definition is as follows:

if(typeof Node == "undefined"){

var Node = {

ELEMENT_NODE: 1,

ATTRIBUTE_NODE: 2,

TEXT_NODE: 3,

CDATA_SECTION_NODE: 4,

ENTITY_REFERENCE_NODE: 5,

ENTITY_NODE: 6,

PROCESSING_INSTRUCTION_NODE: 7,

COMMENT_NODE: 8,

DOCUMENT_NODE: 9,

DOCUMENT_TYPE_NODE: 10,

DOCUMENT_FRAGEMENT_NODE: 11,

NOTATION_NODE: 12

}

}

The test is as follows:

alert(document.nodeType == Node.DOCUMENT_NODE);  // -----> Output: true

Note: The above Node definition can be used to correct that IE does not support DOM Node type constants and is supported by other mainstream browsers ).

Similar to C language, the enumerated values in the preceding two examples are integer values. You might think, can it be defined as another type? To answer this question, we must first understand the principle of enumeration implementation. As mentioned above, JSON is used here, And JSON can use any type of value! Therefore, enumeration in Js can be any type of value. The following uses the String type as an example:

if(typeof Color == "undefined"){

var Color = {

Color1: 'red',

Color2: 'green',

Color3: 'white',

Color4: 'black'

}

}

The test is as follows:

alert(Color.Color1); // -----> Output: red

To define a PersonList with a more complex type, the enumeration is as follows:

if(typeof PersonList == "undefined"){

var PersonList = {

ZhangSan: {

Id: 1,

Name: 'ZhangSan',

Gender: 'man'

},

LiSi: {

Id: 2,

Name: 'LiSi',

Gender: 'woman'

},

ZhaoWu: {

Id: 3,

Name: 'ZhaoWu',

Gender: 'man'

}

}

}

The test result is as follows:

  1. Comparison of the performance of JavaScript-parsed Json strings in various browsers
  2. Javascript binds events cyclically using closures
  3. Horizontal comparison of Web chart frameworks based on JavaScript and CSS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.