How to simulate enumeration with JS

How to simulate enumeration with JS

Preface

In current JavaScript, there is no concept of enumeration. In some scenarios, using enumeration can better ensure the correctness of data and reduce the data verification process. The following introduces how JavaScript can simulate the enumeration effect.

Enumeration main features

  • Enumeration values ​​cannot be repeated
  • Cannot be modified

accomplish

let days;
(function (days) {
    days[days["Sunday"] = 0] = "Sunday";
    days[days["Monday"] = 1] = "Monday";
    days[days["Tuesday"] = 2] = "Tuesday";
    days[days["Wednesday"] = 3] = "Wednesday";
    days[days["Thursday"] = 4] = "Thursday";
    days[days["Friday"] = 5] = "Friday";
    days[days["Saturday"] = 6] = "Saturday";
})(days || (days = {}));

At this point, the days object basically implements the enumeration effect. You can access the days object through the enumeration value or index, but an important feature of the enumeration is that it cannot be modified. At this point, days can be modified at will; you can use the Object.freeze function to prevent the object from being modified and re-export an unmodifiable object. The complete code is as follows:

let days;
(function (days) {
    days[days["Monday"] = 1] = "Monday";
    days[days["Tuesday"] = 2] = "Tuesday";
    days[days["Wednesday"] = 3] = "Wednesday";
    days[days["Thursday"] = 4] = "Thursday";
    days[days["Friday"] = 5] = "Friday";
    days[days["Saturday"] = 6] = "Saturday";
    days[days["Sunday"] = 0] = "Sunday";
})(days || (days = {}));

//Export enumerable object export EnumWeek = Object.freeze(days)

The above is the details of how to use JS to simulate enumeration. For more information about JS, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Parsing js enumerable and non-enumerable attributes through examples
  • JavaScript enumeration selection jquery plug-in code example
  • JavaScript enum enumeration type definition and usage
  • Node.JS enumerates and counts the number of lines of all code files in the current folder and subdirectories
  • Example code for implementing Emrips anti-prime enumeration in javascript
  • JavaScript learning notes_Simple implementation of enumeration type, poker application
  • JS object property related (checking properties, enumerating properties, etc.)
  • In-depth analysis of enumeration functions in JavaScript
  • Simple example of javascript emulating enumeration
  • Functions to enumerate JavaScript objects

<<:  Install Python 3.6 on Linux and avoid pitfalls

>>:  Four ways to compare JavaScript objects

Recommend

Example of automatic import method of vue3.0 common components

1. Prerequisites We use the require.context metho...

Installation, configuration and use of process daemon supervisor in Linux

Supervisor is a very good daemon management tool....

JS implements a simple todoList (notepad) effect

The notepad program is implemented using the thre...

Detailed explanation of HTML form elements (Part 2)

HTML Input Attributes The value attribute The val...

Vue.js handles Icon icons through components

Icon icon processing solution The goal of this re...

Web design dimensions and rules for advertising design on web pages

1. Under 800*600, if the width of the web page is...

How to install redis5.0.3 in docker

1. Pull the official 5.0.3 image [root@localhost ...

JS implements array filtering from simple to multi-condition filtering

Table of contents Single condition single data fi...

Server concurrency estimation formula and calculation method

Recently, I need to stress test the server again....

Implementation of CSS child element selection parent element

Usually a CSS selector selects from top to bottom...

mysql-canal-rabbitmq installation and deployment super detailed tutorial

Table of contents 1.1. Enable MySQL binlog 1.2. C...

HTML exceeds the text line interception implementation principle and code

The HTML code for intercepting text beyond multipl...

Solve the problem when setting the date to 0000-00-00 00:00:00 in MySQL 8.0.13

I just started learning database operations. Toda...

More Ways to Use Angle Brackets in Bash

Preface In this article, we will continue to expl...

Use Navicate to connect to MySQL on Alibaba Cloud Server

1. First enter the server's mysql to modify p...