A simple example of a JavaScript mock enumeration

Source: Internet
Author: User
Tags json

  This article is mainly on the JavaScript simulation enumeration of simple examples of the introduction, the need for friends can come to the reference, I hope to help you.

As follows, we define the enumeration of week:     code below: if (typeof weekday = = "undefined") {    var weekday = {};   Weekday.sund ay = 0;   Weekday.monday = 1;   Weekday.tuesday = 2;   Weekday.wedesay = 3;   Weekday.thursday = 4;   Weekday.friday = 5;   Weekday.saturday = 6;  }     tests are as follows: Alert (weekday.monday);  //-----> output:1    Of course, we have a more intuitive approach. For example, define the DOM document node type as follows:   Code as follows: if (typeof Node = = "undefined") {    var node = {  Element_node:1, &nbsp ; 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  }     test as follows: The code is as follows: Alert (Document.nodetype = = Node.document_node);  //-----> output:true    Description, the above node definition can be used as correction IE does not support DOM node type constants (OtherMainstream browsers are supported).   Similar to the Class C language, the enumerated corresponding property values for the above 2 examples are all cosmetic. You might think, could it be defined as another type? To answer this question, we must first know the principle of our enumeration implementation. As mentioned earlier, this is achieved with JSON, and JSON can use any type of value! Therefore, the enumeration in JS can be any type of value. The following is an example of string type:     code is as follows: if (typeof Color = "undefined") {    var color = {    Color1: ' Red ', & nbsp   Color2: ' Green ',    Color3: ' White ',    Color4: ' Black '  } }     test is as follows: Ale RT (Color.color1); -----> output:red    Define a Personlist enumeration as follows in a more complex type:   Code as follows: if (typeof personlist = = "undefined") {& nbsp   var personlist = {  Zhangsan: {  id:1,   Name: ' Zhangsan ',   Gender: ' Man '  },   LiSi: {  Id:2,   name: ' LiSi ',   Gender: ' Woman '  },   Zhaowu: {  Id:3,   name: ' Z Haowu ',   Gender: ' Man '  }}}  
Related Article

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.