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

js canvas realizes circular water animation

This article example shares the specific code of ...

How to install golang under linux

Go is an open source programming language that ma...

MySQL database case sensitivity issue

In MySQL, databases correspond to directories wit...

Server concurrency estimation formula and calculation method

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

Detailed explanation of two quick ways to write console.log in vscode

(I) Method 1: Define it in advance directly in th...

CSS implements six adaptive two-column layout methods

HTML structure <body> <div class="w...

An example of how to implement an adaptive square using CSS

The traditional method is to write a square in a ...

A complete record of a Mysql deadlock troubleshooting process

Preface The database deadlocks I encountered befo...

Vue v-model related knowledge summary

​v-model is a Vue directive that provides two-way...