convert from if else to switch statement
I have the following if, else if, else construct and I am just curious how I could convert such as construct into a switch statement.
var emailSubject = email.subject.toLowerCase();
if(emailSubject.indexOf(开发者_如何学Go"account request") >= 0){
//do acct req
}else if(emailSubject.indexOf("accounts pending removal for") >= 0){
//do account removal
}else if(emailSubject.indexOf("listserv application") >= 0){
//do listserv app
}else if(emailSubject.indexOf("student organization webmaster transfer request") >= 0){
//do webmaster xfer
}else{
//do default
}
My thoughts are but I do not think this is correct:
switch(emailSubject){
case this.indexOf("account request"):
//do acct request
break;
default:
//do default
}
Or
switch(0){
case emailSubject.indexOf("accounts pending removal"):
//process account pending removal
break;
default:
//do default behavior
}
Your example code cannot easily be converted to a switch statement in most languages, nor should it. switch
is for comparing a single variable against a range of constant values, whereas your logic requires comparison against non-constant values, with no variable to compare them with. if
/else if
is the correct construction for your case.
You can only use case to check a value:
switch(emailSubject){
case "Subject1": //(emailSubject == "Subject1")
//do acct request
break;
case "Subject2": //(emailSubject == "Subject2")
//do something else
break;
default:
//do default
}
Otherwise you should be using if/else
Constructs like this are usually crying out for polymorphism...
Play with it here: http://jsbin.com/utilu4/3
var mailHandlers = [
{
CanHandleEmail : function(email) {
return email.subject.toLowerCase().indexOf("account request") >= 0;
},
HandleEmail : function(email) {
alert("do acct req");
}
},
{
CanHandleEmail : function(email) {
return email.subject.toLowerCase().indexOf("account pending removal for") >= 0;
},
HandleEmail : function(email) {
alert("do account removal");
}
},
{
CanHandleEmail : function(email) {
return email.subject.toLowerCase().indexOf("listserv application") >= 0;
},
HandleEmail : function(email) {
alert("do listserv app");
}
},
{
CanHandleEmail : function(email) {
return email.subject.toLowerCase().indexOf("student organization webmaster transfer request") >= 0;
},
HandleEmail : function(email) {
alert("do webmaster xfer");
}
},
{
CanHandleEmail : function(email) {
return true;
},
HandleEmail : function(email) {
alert("do default");
}
}
];
function HandleEmail(email) {
for(i=0; i< mailHandlers.length; i++) {
if(mailHandlers[i].CanHandleEmail(email)){
mailHandlers[i].HandleEmail(email);
break;
}
}
};
As mentioned, if/else is best for what you've got.
If, however, you were looking for actual whole subject lines, instead of words within subject lines, you could do something like:
var a = ["account request", "listserv application", "student organization webmaster transfer request"];
switch(a.indexOf(emailSubject)) {
// ...
}
Just as a tip: wrap your code in a function and return the value of the match. (You don't have to use else in this case.) If you want to, you could return a code for the match (eg. an int) and use switch/case to perform the action.
I've just come across this in the wild and I can't help but share it, but don't do it.
var emailSubject = email.subject.toLowerCase();
switch (true) {
case (emailSubject.indexOf("account request") >= 0):
//do acct req
break;
case (emailSubject.indexOf("accounts pending removal for") >= 0):
//do account removal
break;
case (emailSubject.indexOf("listserv application") >= 0):
//do listserv app
break;
case (emailSubject.indexOf("student organization webmaster transfer request") >= 0):
//do webmaster xfer
break;
default:
//do default
break;
}
精彩评论