use*_*335 2 javascript if-statement
我只是一个javascript的新手,这就是我如何在javascript中写条件,
function setAccType(accType) {
if (accType == "PLATINUM") {
return "Platinum Customer";
} else if (accType == "GOLD") {
return "Gold Customer";
} else if (accType == "SILVER") {
return "Silver Customer";
}
},
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法呢?
您可以将对象用作地图:
function setAccType(accType){
var map = {
PLATINUM: 'Platinum Customer',
GOLD: 'Gold Customer',
SILVER: 'Silver Customer'
}
return map[accType];
}
Run Code Online (Sandbox Code Playgroud)
或者@Tushar指出:
var accountTypeMap = {
PLATINUM: 'Platinum Customer',
GOLD: 'Gold Customer',
SILVER: 'Silver Customer'
}
function setAccType(accType){
return accountTypeMap[accType];
}
Run Code Online (Sandbox Code Playgroud)