当前位置:网站首页>How to define an "enumeration" type in JS

How to define an "enumeration" type in JS

2022-06-23 21:02:00 User 1349575

Javascript1.8.5 You can use Object.freeze To freeze objects to implement a similar ” enumeration ” type .

The implementation code is as follows :

/**
 *  Time :2019 year 8 month 18 Japan 
 *  Front end tutorial : https://www.pipipi.net/ 
 */
var DaysEnum = Object.freeze({"monday":1, "tuesday":2, "wednesday":3, ...})

Or so :

/**
 *  Time :2019 year 8 month 18 Japan 
 *  Front end tutorial : https://www.pipipi.net/ 
 */
var DaysEnum = {"monday":1, "tuesday":2, "wednesday":3, ...}
Object.freeze(DaysEnum)

This is it. js Medium ” enumeration ”! Is it simple ?

But now it's 2019 Years. , We can also use const To define the object .

Complete code

The complete implementation code is as follows :

/**
 *  Time :2019 year 8 month 18 Japan 
 *  Front end tutorial : https://www.pipipi.net/ 
 */
var Status = Object.freeze({
    "Connecting":0,
    "Ready":1,
    "Loading":2,
    "Processing": 3
});

console.log(Status.Ready) // 1

console.log(Object.keys(Status)[Status.Ready]) // Ready
原网站

版权声明
本文为[User 1349575]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/12/202112281257055356.html