Problem with switch
I have this code:
var str = $("#datepicker").datepicker("getDate");
var datestr = str.toString().split(" ");
switch(datestr[1]) {
case "Jan":
var datestrmon = "Яну";
break;
case "Feb":
var datestrmon = "Фев";
break;
case "Mar":
var datestrmon = "Мар";
break;
case "Apr":
var datestrmon = "Апр";
break;
case "May":
var datestrmon = "Май";
break;
case "Jun":
开发者_运维技巧 var datestrmon = "Юни";
break;
case "July":
var datestrmon = "Юли";
break;
case "Aug":
var datestrmon = "Авг";
break;
case "Sep":
var datestrmon = "Сеп";
break;
case "Oct":
var datestrmon = "Окт";
break;
case "Nov":
var datestrmon = "Ное";
break;
case "Dec":
var datestrmon = "Дек";
break;
default:
datestr[1];
}
alert(datestrmon);
I expect when the browser is alerting me datestrmon to alert "Юли" ,but it alerts me "undefined".Why?
- Are you sure it's "July" instead of "Jul"?
- Your
default:
case did not assigndatestr[1]
todatestrmon
. Is it intentional?
Anyway, consider using a lookup table instead of a big switch.
var datestrmon = ({"Jan": "Яну",
"Feb": "Фев",
...
"Dec": "Дек"})[datestr[1]] || datestr[1];
// ^^^^^^^^^^^^^
// optional, if you think stuff outside of
// the table could appear and need a
// fallback.
Try defining datestrmon
outside the case. Below is some cleaned up code. If it doesn't work, please post your input.
var str = $("#datepicker").datepicker("getDate");
var datestr = str.toString().split(" ");
var datestrmon = "";
switch(datestr[1]) {
case "Jan": {
datestrmon = "Яну";
break;
}
case "Feb": {
datestrmon = "Фев";
break;
}
case "Mar": {
datestrmon = "Мар";
break;
}
case "Apr": {
datestrmon = "Апр";
break;
}
case "May": {
datestrmon = "Май";
break;
}
case "Jun": {
datestrmon = "Юни";
break;
}
case "July": {
datestrmon = "Юли";
break;
}
case "Aug": {
datestrmon = "Авг";
break;
}
case "Sep": {
datestrmon = "Сеп";
break;
}
case "Oct": {
datestrmon = "Окт";
break;
}
case "Nov": {
datestrmon = "Ное";
break;
}
case "Dec": {
datestrmon = "Дек";
break;
}
default: {
datestr[1];
}
}
alert(datestrmon);
精彩评论