私はGoogleマップのAPIガイドを見て、このページのデモでは、実際に呼び出さずに、明らかにinitMap()内で定義された関数を使用していることに気付きました。私が見つけることができる唯一の関連情報は、変数として定義したり渡したりするときに、関数の最後に誤ってa()を追加している人ですが、ここでは起こりません。これらの関数はどのように実行されていますか?関数は定義時に実行されていますか? -JS
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
がcallback
パラメータを参照してください:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {lat: -28.024, lng: 140.887}
});
// Create an array of alphabetical characters used to label the markers.
var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
// Add some markers to the map.
// Note: The code uses the JavaScript Array.prototype.map() method to
// create an array of markers based on a given "locations" array.
// The map() method here has nothing to do with the Google Maps API.
var markers = locations.map(function(location, i) {
return new google.maps.Marker({
position: location,
label: labels[i % labels.length]
});
});
// Add a marker clusterer to manage the markers.
var markerCluster = new MarkerClusterer(map, markers,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
}
渡された要素ファインダを使ってコンストラクタ内の変数を設定すると、関連付けを介してページが設定されるということですか?単にオブジェクトを設定するだけで、関数は実際に呼び出し/実行されませんか? –
私は今あなたが言っていることを見て、コンストラクタは単に新しいオブジェクトを作成するのではなく、マップを作成して適用することによって他の作業を行うことです - コンストラクタは新しく作成されたオブジェクト 'map'それは「新しい」を通して呼び出されています。正しい? –
ええ、それは基本的にそれです。あなたはOO JSを読んでこれについてもっと知ることができます。 –