使用JS枚举简化if else



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset="utf-8"">
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td><input type="button" value="按钮" onClick="getMsg('0000')"></td>
</tr>
</table>

</body>
</html>

<script>
//使用枚举简化状态码判断
function getMsg(key) {
var codeEnum = {"0000":"登录成功", "0001":"aaaa", "0002":"bbbb", "0003":"cccc"};
alert(codeEnum[key]);
}
</script>

你可能感兴趣的:(js 枚举 if else)