所以我在我的页面的 javascript 中有这个:
var TEST_ERROR = {
'SUCCESS' : 0,
'FAIL' : -1,
'ID_ERROR' : -2
};
并对页面中的功能进行测试,如下所示:
function test()
{
// Get the paragraph from the DOM
var testResultParagraph = document.getElementById('testResult');
// Check the paragraph is valid
if(!testResultBox)
{
// Update the web page
testResultParagraph.value = TEST_ERROR.ID_ERROR;
return TEST_ERROR.ID_ERROR;
}
// Something to store the results
var testResult = TEST_ERROR.SUCCESS;
// Test the calculation
testResult = testCalculate()
// Update the web page
testResultParagraph.value = testResult;
// The test succeeded
return TEST_ERROR.SUCCESS;
}
testCalculate()
的结果和段落的值将是0、-1、-2,具体取决于结果。
现在我想将其映射到一个字符串,以便该段落显示“成功”、“失败”或“ID错误”
我可以通过我想到的几种方法来做到这一点:
var TEST_ERROR = {
'SUCCESS' : {VALUE : 0 , STRING: 'Success' },
'FAIL' : {VALUE : -1, STRING: 'Fail' },
'ID_ERROR' : {VALUE : -2, STRING: 'Id Error'},
};
需要修改枚举点访问器,或者
var TEST_ERROR = {
'SUCCESS' : 0,
'FAIL' : 1,
'ID_ERROR' : 2
};
var TEST_STRING = [
'Success',
'Fail',
'ID Error'
];
这需要改变逻辑(
result > TEST_ERROR.SUCCESS
看起来很奇怪!)
我的问题是如何将枚举器值映射到Javascript中的字符串值?我认为第二种方法是最明智的,但希望枚举器对于成功为正,对于失败为负。我也喜欢第一个包含对象结构中的字符串和值的想法。
有什么想法吗?
谢谢!
马特
PS。我将在 Web Worker 中进行测试,这样页面就不会挂起,结果将放入表格中,而不是像上面那样放入段落中。
PPS。我对 Javascript 编程还很陌生,但在 ASM、C、C++、C# 方面做了很多工作。
您真的需要这些数值吗?如果没有,那么你可以使用这样的东西:
const TEST_ERROR = {
SUCCESS : 'Success',
FAIL : 'Fail',
ID_ERROR : 'ID Error'
};
Object.freeze(TEST_ERROR)
不是最理想的,但是在不预先计算反向字典的情况下可以获得的最干净的结果(此外,如果你只有几个枚举值,这不应该是一个太大的问题):
function string_of_enum(enum,value)
{
for (var k in enum) if (enum[k] == value) return k;
return null;
}
对于 TypeScript,有一个更简单的解决方案 (如果你坚持使用JS,请忽略我的回答):
export enum Direction {
none,
left = 1,
right = 2,
top = 4,
bottom = 8
}
export namespace Direction {
export function toString(dir: Direction): string {
return Direction[dir];
}
export function fromString(dir: string): Direction {
return (Direction as any)[dir];
}
}
console.log("Direction.toString(Direction.top) = " + Direction.toString(Direction.top));
// Direction.toString(Direction.top) = top
console.log('Direction.fromString("top") = ' + Direction.fromString("top"));
// Direction.fromString("top") = 4
console.log('Direction.fromString("xxx") = ' + Direction.fromString("unknown"));
// Direction.fromString("xxx") = undefined
因为枚举类型被编译成对象(字典)。 您不需要循环来找到相应的值。
enum Direction {
left,
right
}
编译为:
{
left: 0
right: 1
0: "left",
1: "right"
}
我更喜欢下面的方法。
enum ColorEnum {
Red,
Green,
Blue
}
console.log(ColorEnum[ColorEnum.Red]);
您始终可以将值设为特定类型的对象。
var TEST_ERROR = (function() {
function ErrorValue(value, friendly) {
this.value = value;
this.friendly = friendly;
}
ErrorValue.prototype = {
toString: function() { return this.friendly; },
valueOf: function() { return this.value; }
};
return {
'SUCCESS': new ErrorValue(0, 'Success'),
'FAIL': new ErrorValue(1, 'Fail'),
'ID_ERROR': new ErrorValue(2, 'ID error')
};
})();
现在,当您获得该类型的值时:
var err = testFunction(whatever);
您可以使用
获取字符串值alert(err.toString());
事实上,大多数时候你甚至不需要显式地调用
.toString()
。
如果您是打字稿,那么您已经有了枚举的定义。否则你可以直接使用 JS 版本的枚举。
var Status;
(function (Status) {
Status[Status["New"] = 0] = "New";
Status[Status["Submitted"] = 1] = "Submitted";
Status[Status["Approved"] = 2] = "Approved";
Status[Status["Rejected"] = 3] = "Rejected";
})(Status || (Status = {}));
var snew = Status.New;
console.log(snew); //This is the number
console.log(Status[snew]); //This is the string
最好的方法是:
export const UserLevel = Object.freeze({
BABY: 1,
CHILED: 2
});
我们需要添加冻结,即使
UserLevel
是const
,因为我们可以更改里面的值。 freeze
将使“枚举”免受更改。
这可能与您想要的不同,但我想分享我的答案。这是受到@LukeHconst 解决方案的启发。考虑下面的
bookCategory
,您会注意到我使用数字作为键。
const bookCategory = {
"0": "Biography",
"1": "Fiction",
"2": "History",
"3": "Mystery",
"4": "Suspense",
"5": "Thriller"
};
我这样写
bookCategory
是因为如果你在MySQL中使用枚举列。例如,
category ENUM ('0', '1', '2', '3', '4', '5')
您需要在 JavaScript 中进行某种转换。所以我想出了这个,用法很简单:
bookCategory[book.category]
如果您使用 TypeScript 并且不需要数值,则可以直接映射到字符串值,如下所示:
enum TEST_ERROR {
SUCCESS = 'Success',
FAIL = 'Fail',
ID_ERROR = 'ID Error'
}
来源:https://www.typescriptlang.org/docs/handbook/enums.html#string-enums
基于 Victor 的出色答案,因此它可以在 TypeScript 中运行:
enumToStr(enumeration: any, value: any): string {
for (var k in enumeration)
if (enumeration[k] == value)
return <string>k;
return null;
}