PrefaceIn 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
accomplishlet 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:
|
<<: Install Python 3.6 on Linux and avoid pitfalls
>>: Four ways to compare JavaScript objects
When configuring proxy_pass in nginx, if you matc...
Today I downloaded mysql-5.7.18-winx64.zip from t...
This article example shares the specific code of ...
Copy code The code is as follows: <HTML> &l...
Go is an open source programming language that ma...
In MySQL, databases correspond to directories wit...
Table of contents 1 Introduction 2 Prerequisites ...
Recently, I need to stress test the server again....
(I) Method 1: Define it in advance directly in th...
HTML structure <body> <div class="w...
The traditional method is to write a square in a ...
Why do we need master-slave replication? 1. In a ...
Preface The database deadlocks I encountered befo...
This article uses examples to illustrate the prin...
v-model is a Vue directive that provides two-way...