Mike, thanks for excellent and very succinct reply. Now I see that it's the programmer's option to properly scope the variables, and not a requirement of the API.
On Sep 2, 11:53 am, Mike Williams <[email protected]> wrote: > Wasn't it d...@mausner who wrote: > > > > >Mappers, every published code sample uses a closure as the > >GEvent.addListener callback. There seem to be multiple ways. Do they > >have different side effects? > > >1. addListener(x, y, function (z) { code }); > >2. function A(z) {code}; addListener(x, y, A); > >3. function A(z) {code}; addListener(x, y, function (z) { A(z) }); > > On their own, those do the same thing, but in some contexts they are > different consider: > > 1. function createMarker(point) { > var marker = new GMarker(point); > GEvent.addListener(marker, y, function (z) { code }); > } > > 2. function A(z) {code} > function createMarker(point) { > var marker = new GMarker(point); > GEvent.addListener(marker, y, A); > } > > 3. function A(z,marker) {code} > function createMarker(point) { > var marker = new GMarker(point); > GEvent.addListener(marker, y, function (z) { A(z,marker) }); > } > > In case #1, the {code} has direct access to all variables that are held > in the function closure of the createMarker() function, that's local > variables like "marker" and passed arguments like "point". > > In case #2, the {code} has no access to the variables in the > createMarker() closure. So it can't do things like > marker.openInfoWindow(). > > In case #3, the anonymous function has access to the variables held in > the closure, so it can pass them to the A() function in that function > call. > > When you're dealing with marker "click" events, you probably do want to > know which marker was clicked, and that information is usually inside > the function closure, so you'd need to use #1 or #3. > > When you're dealing with map "zoomend" events, and only have one map on > your page, you probably don't need any variables to be passed via > closure. You can get all the required info either from the arguments > returned from the event or by making calls like map.getZoom(). So any of > those ways would work. > > -- > Mike Williamshttp://econym.org.uk/gmap --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google Maps API" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/Google-Maps-API?hl=en -~----------~----~----~----~------~----~------~--~---
