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,   ; 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 ' }}}