자바가 제 첫 번째 언어가 아니며 잘 알고 있습니다. 기능상의 멋진 물건, 많은 물건이 아닙니다.전역 변수 감소 및 기능 흐름 유지
누구나 다음 시나리오를 처리하기위한 옵션을 제안 할 수 있는지보고 싶습니다.
function postcodeEntry(destination,postcode) {
"use strict";
document.getElementById('googlemappostcode').innerHTML = <?php if ($_GET['page']==="fun") echo "<div id=\"map\"></div>' + ";?>'<form id="postcodeEntry" method="post" action="/" onsubmit="calcRoute(\'driving\',this.postcode.value,' + destination.hb + ',' + destination.ib + ',\'' + postcode + '\');return false;">'
+ '<h2>Get directions</h2>'
+ '<fieldset>'
+ '<input type="hidden" name="action" value="gmap-directions" />'
+ '<p class="where"><a href="/contact/" onclick="geoLoc();return false;">Where am I?</a></p>'
+ '<label for="postcode">Enter your postcode for directions</label>'
+ '<input type="text" name="postcode" id="postcode" onfocus="checkthis(this,\'Your Postcode/Address:\')" onblur="checkthis(this,\'Your Postcode/Address:\')" value="Your Postcode/Address:" />'
+ '<input type="submit" name="submitTravelType" value="Driving" id="gms_driving" onclick="calcRoute(\'driving\',document.getElementById(\'postcode\').value,' + destination.hb + ',' + destination.ib + ',\'' + postcode + '\');return false;" />'
+ '<input type="submit" name="submitTravelType" value="Walking" id="gms_walking" onclick="calcRoute(\'walking\',document.getElementById(\'postcode\').value,' + destination.hb + ',' + destination.ib + ',\'' + postcode + '\');return false;" />'
+ '<input type="submit" name="submitTravelType" value="Public Transport" id="gms_transit" onclick="calcRoute(\'transit\',document.getElementById(\'postcode\').value,' + destination.hb + ',' + destination.ib + ',\'' + postcode + '\');return false;" />'
+ '<input type="submit" name="submitTravelType" value="Cycling" id="gms_bicycling" onclick="calcRoute(\'bicycling\',document.getElementById(\'postcode\').value,' + destination.hb + ',' + destination.ib + ',\'' + postcode + '\');return false;" />'
+ '</fieldset>'
+ '</form>'
+ '<div id="directions"></div>';
}
function initialize() {
"use strict";
var contentString, destination, el, entryPanoId, i, image, infowindow, links, mapOptions, panoId, panoOptions, radius, shadow, shape;
/* Destination becomes a four part array. 1st being lat, 2nd being long, 3rd being a google maps latlng and the 4th being postcode */
destination = [<?php echo $venue['latlong']?>];
destination[2] = new google.maps.LatLng(destination[0], destination[1]);
destination[3] = '<?php echo $venue['postcode']?>';
postcodeEntry(destination[2], destination[3]);
directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();
trafficLayer = new google.maps.TrafficLayer();
streetviewService = new google.maps.StreetViewService();
...
초기화 기능이 호출되면 비트가되어 맵을 잘 만듭니다. 우편 번호/위치 항목이 완벽하게 작동합니다. 사실 그것은 모든 문제없이 작동합니다.
제 질문은 일부 정보가 우편 번호 양식에 저장되어 있기 때문에 해당 양식의 반환은 geoLoc 또는 calcRoute 기능으로 이동합니다. 물론 이것은 초기화 기능에서 설정 한 것과의 접촉을 잃는 것을 의미합니다.
이것은 표시된 스크립트 세그먼트의 모든 항목을 포함하여 많은 수의 전역 변수를 사용해야 함을 의미합니다.
나는 언어에 대해 더 많이 배우고 그것의 사용에 능숙 해지고있다. 결과적으로, (나는 순수한 완고함에서 벗어나서) 전역을 아무도 (가능한 경우) 또는 가능한 한 적게 줄이기 위해 노력하고 싶습니다.
해결 방법이 있습니까? 나는 자바 스크립트 객체에 대해 많이 알지는 못하지만 모든 시간을 배우기 때문에 어떻게 초기화 함수를 다른 객체를 포함하는 객체로 바꿀 수 있습니까? (현재 자바 스크립트 객체에 대해 거의 알지 못하는 것을 고려하면 실제로 완전히 미칠 수도 있습니다.)
감사합니다 :) 내가 아주 의도적으로 HTML 코드를 생성하는 JS를 사용하여 실제로 나는 때문에 자바 스크립트가 꺼져 있거나 사용 가능하지 않을 때 다른 옵션을 사용할 수 있다는 의미의 다른 부분이 있습니다. 사용 가능한 경우 js의 대화식 기능으로 대체됩니다. 그렇게하는 것은 의식적인 결정입니다. PHP에 관해서는 개인적인 것 같아요. 한 가지 질문입니다. 양식이 게시해야하는 기능을 어떻게 처리합니까? – Rafe
방금 내 대답에 몇 가지 추가 예제를 추가했습니다. 희망이 도움이됩니다. –
이것은 갈 길과 같습니다. 어제 무거운 학습 곡선 후에 기능의 절반 만 변환했습니다. 나는 몇몇 조회가있다 그러나 나가 응답을 나 자신 찾아 낼 수없는 경우에 나는 다른 질문을 위해 그 (것)들을 저장할 것이다. 고마워요 :) – Rafe