if语句中多个OR表达式的简写
是否有以下简写 –
if(tld == "com" || tld == "net" || tld == "co" || tld == "org" || tld == "info" || tld == "biz") { //do something; }
你可以使用一个数组
if(["","com","net","co","org","info","biz"].indexOf(tld) > -1) { // do something }
或者如果你使用jquery:
$.inArray(tld, ["com","net","co","org","info","biz"])
REF – OR运算(||)与inArray()的表现
使用正则表达式:
if ( /^(com|net|co|org|info|biz)$/i.test(tld) ) { // do something }
你有没有想过使用switch语句? 像这样的东西:
switch(tld) { case 'com': case 'net': case 'co': ... ... // do something for all of them break; default: // if you want you can have default process here break; }