This code from Google Maps documentation page is quite a mystery to me. The functions in the snippet refer to an object google and everything is working great.
I expected then that the object google exists at a global scope, since it is being used by global functions, who otherwise have no way of knowing about it (i.e. they are not declaring it, and it is not being passed in as a parameter)
For example if I see code like
function () {
// do some calc
anotherFunction(resultOfCalc)
...
}
I would assume that anotherFunction is available in at least the parent scope - if not the global one.
Here is the JSFiddle for the Google code. Notice I added a line console.log("Google obj:", google);
in the console it is printing
VM573:66 Uncaught ReferenceError: google is not defined
at VM573:66
But why? The next lines in the code are
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: {lat: -33.9, lng: 151.2}
});
setMarkers(map);
}
and there is no compaint about the use of google.
Please help me understand this peculiar behaviour.