xkkui
New Coder
Hi! Every body, I found a lot of articles, how to define enum in JavaScript/Node.js.
Most of the answer that is usage closure function.
But I found the article that is usage class.
The following code:
I try using the code. But I discover that static field or field of instance can be changed on outside of module.
e.g.:
Refer: https://www.sohamkamani.com/javascript/enums/
Most of the answer that is usage closure function.
But I found the article that is usage class.
The following code:
JavaScript:
"use strict";
class GenderTypeEnum {
static SECRECY = new GenderTypeEnum(0, "保密");
static MALE = new GenderTypeEnum(1, "男");
static FEMALE = new GenderTypeEnum(2, "女");
constructor (code, name) {
this.code = code;
this.name = name;
}
}
module.exports = GenderTypeEnum;
I try using the code. But I discover that static field or field of instance can be changed on outside of module.
e.g.:
Code:
const GenderTypeEnum = require("./GenderTypeEnum");
console.log(GenderTypeEnum.MALE.code); /// 1
GenderTypeEnum.MALE.code = 4;
console.log(GenderTypeEnum.MALE.code); /// 4
console.log(GenderTypeEnum.FEMALE); /// GenderTypeEnum { code: 2, name: '女' }
GenderTypeEnum.FEMALE = "female";
console.log(GenderTypeEnum.FEMALE); /// female
Refer: https://www.sohamkamani.com/javascript/enums/