다음 코드가 있습니다. 어떤 이유로 실행하고 싶지 않은 case 문 (Case '2'의 경우 두 번째)이 올바른 항목 (Case '4')과 함께 실행되고 있습니다. 온라인으로 읽은 후 대부분의 ppl은 "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);
오류 정보
마침내 친구의 도움으로 내 문제를 해결했습니다. Javascript는 초기화되지 않은 case 문 내부의 변수를 좋아하지 않습니다. case 문 중 하나에서 이전에 일부 논리를 기반으로 값을 할당했던 변수를 사용했습니다. 이 case 문이 "true"로 평가되지 않았지만 내 프로그램에서 오류가 발생했습니다.
예를 들어 아래 코드에서 프로그램은 m이 "false"인 경우에도 b가 공백 / null 인 경우 n 및 m에 대해 오류가 발생합니다.
switch(expression) {
case n:
break;
case m:
Var a=b;
break;
default:
default code block
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다