所以我在我的页面的javascript中有这个:
var TEST_ERROR = {
'SUCCESS' : 0,
'FAIL' : -1,
'ID_ERROR' : -2
};
Run Code Online (Sandbox Code Playgroud)
并对页面中的函数执行测试,如下所示:
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; …Run Code Online (Sandbox Code Playgroud) 考虑以下示例.
enum DialogType {
Options,
Help
}
class Dialog {
test() : string {
return "";
}
}
class Greeter {
openDialogs: { [key in DialogType]: Dialog | undefined } = {
0: undefined,
1: undefined
};
getDialog(t: DialogType) {
return this.openDialogs[t];
}
}
const greeter = new Greeter();
const d = greeter.getDialog(DialogType.Help);
if (d) document.write(d.test());
Run Code Online (Sandbox Code Playgroud)
它有3个问题/问题:
问题是可以枚举用作键类型而不仅仅是"数字"或"字符串"?目前似乎唯一可能的声明是x:{[key:number]:any}其中key可以是"number"或"string"类型.有可能在这个例子中做出类似的东西:
例:
enum MyEnum
{
First,
Second
}
var layer:{[key:MyEnum]:any};
Run Code Online (Sandbox Code Playgroud) 我正在使用TypeScript 制作RAM机器模拟器,因此我制作了RAM可以具有的指令类型的枚举:
enum InsType {
LOAD, // Put value from specified register (or literal) into accumulator.
STORE, // Store value from accumulator into specified register.
READ, // Read from input tape and write into specified register.
WRITE, // Write to output tape from specified register.
ADD, // Add value into accumulator.
SUB, // Subtract value from accumulator.
MUL, // Multiply accumulator by referenced (or literal) value.
DIV, // Divide accumulator by referenced (or literal) value.
HALT, // Stop …Run Code Online (Sandbox Code Playgroud)