我有以下代码。出于某种原因,我不想执行的案例陈述(第二个案例' 2')除了正确的案例之外还要执行(Case' 4')。在线阅读后,大多数人都可以通过添加" break"来解决类似的问题。声明。但它并不适合我。请指教。以下输出来自我的浏览器
var chk = '4'
switch (chk) {
case '4':
var locations = [
["936001_STURGEON_BAY_MEYER", 44.8358, -87.3305, "LRA", 1],
["936087_SHADOW_LAKE", 45.2183, -88.5981, "LRA", 2],
["936136_PIG", 44.5925, -88.0808, "OMS", 3],
["936136_PIG", 44.5925, -88.0808, "OMS", 4]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: new google.maps.LatLng(locations[0][1], locations[0][2]),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow({
maxWidth: 400000
});
var locations_all_cells1 = locations;
var marker, i;
for (i = 0; i < locations_all_cells1.length; i++) {
var type1 = locations_all_cells1[i][3];
switch (type1) {
case "OMS":
marker1 = new google.maps.Marker({
position: new google.maps.LatLng(locations_all_cells1[i][1], locations_all_cells1[i][2]),
map: map,
icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png'
});
google.maps.event.addListener(marker1, 'click', (function(marker1, i) {
return function() {
infowindow.setContent(locations_all_cells1[i][0]);
infowindow.open(map, marker1);
}
})(marker1, i));
break;
case "LRA":
marker2 = new google.maps.Marker({
position: new google.maps.LatLng(locations_all_cells1[i][1], locations_all_cells1[i][2]),
map: map,
icon: 'http://maps.google.com/mapfiles/ms/icons/red-dot.png'
});
google.maps.event.addListener(marker2, 'click', (function(marker2, i) {
return function() {
infowindow.setContent(locations_all_cells1[i][0]);
infowindow.open(map, marker2);
}
})(marker2, i));
break;
case "UPSAVE":
marker3 = new google.maps.Marker({
position: new google.maps.LatLng(locations_all_cells1[i][1], locations_all_cells1[i][2]),
map: map,
icon: 'http://maps.google.com/mapfiles/ms/icons/yellow-dot.png'
});
google.maps.event.addListener(marker3, 'click', (function(marker3, i) {
return function() {
infowindow.setContent(locations_all_cells1[i][0]);
infowindow.open(map, marker3);
}
})(marker3, i));
}
}
break;
case '2':
var locations = [
["936001_STURGEON_BAY_MEYER", 44.8358, -87.3305, "LRA", 1],
["936087_SHADOW_LAKE", 45.2183, -88.5981, "LRA", 2],
["936136_PIG", 44.5925, -88.0808, "OMS", 3],
["936136_PIG", 44.5925, -88.0808, "OMS", 4]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: new google.maps.LatLng(locations[0][1], locations[0][2]),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow({
maxWidth: 400000
});
var locations_all_cells = ;
var marker, i;
for (i = 0; i < locations_all_cells.length; i++) {
var type1 = locations_all_cells[i][3];
}
break;
default:
text = "Looking forward to the Weekend";
}
console.log(text);
&#13;
错误信息
答案 0 :(得分:1)
switch语句首先计算其表达式。然后,它查找第一个case子句,其表达式的计算结果与输入表达式(使用严格比较,===)的结果相同,并将控制转移到该子句,执行关联的语句。
switch
执行严格检查,与您的'4'
(字符串)和4
(数字)不匹配。所以你需要改变你的代码,如:
var chk = '4';
<强>更新强>
如果您的浏览器确实正确加载了它,例如chk = '4'
,那么它应该可以正常运行。见这里:
var chk = '4';
// Added this for checking.
var type1 = 'OMS';
switch (chk) {
case '4':
switch (type1) {
case "OMS":
text = "4 - OMS";
break;
case "LRA":
text = "4 - LRA";
break;
case "UPSAVE":
text = "4 - UPSAVE";
}
break;
case '2':
// ####Do some Stuff here##
break;
default:
text = "Looking forward to the Weekend";
}
console.log(text);
答案 1 :(得分:0)
我终于在朋友的帮助下解决了我的问题。事实证明,Javascript不喜欢未初始化的case语句中的变量。在其中一个case语句中,我使用的是一个变量,我之前根据某些逻辑为其分配了一个值。尽管此案例陈述未被评估为&#34; true&#34;我的程序抛出错误。
例如,在下面的代码中,如果b为空/空,则程序n和m会出错,即使m是&#34; false&#34;:
switch(expression) {
case n:
break;
case m:
Var a=b;
break;
default:
default code block
}