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
1. Prerequisites We use the require.context metho...
Supervisor is a very good daemon management tool....
The notepad program is implemented using the thre...
HTML Input Attributes The value attribute The val...
Icon icon processing solution The goal of this re...
1. Under 800*600, if the width of the web page is...
1. Pull the official 5.0.3 image [root@localhost ...
Table of contents Single condition single data fi...
Recently, I need to stress test the server again....
Usually a CSS selector selects from top to bottom...
Table of contents 1.1. Enable MySQL binlog 1.2. C...
The HTML code for intercepting text beyond multipl...
I just started learning database operations. Toda...
Preface In this article, we will continue to expl...
1. First enter the server's mysql to modify p...