[Javascript] Anonymous functions

Natalie Downe natbat at gmail.com
Tue Dec 4 18:21:45 CST 2007


Hello,

> > I would like to take the first function() {...} and make it a named
> > function, but when I do it I get something like map undefined. How
> > do I un-anonymous-ize a function?

Where is map being defined? if you do take it out of this add listener
call make sure you put it after where 'map' is specified, this could
be why you are getting 'map undefined errors. So your code would look
something like


// somewhere where map is defined

// taking out your function into a named function
function myNamedFunction () {
   document.getElementById("mapinfo").innerHTML="Zoom:
"+map.getZoom()+" Bounds: "+map.getBounds();
   GDownloadUrl(url, function(data, status) {
       markers=eval(data);
       map.clearOverlays();
       for (var i in markers) {
           var where = new GLatLng(markers[i].lat, markers[i].lng);
           var opts = {title: markers[i].title};
           var marker = new GMarker(where, opts);
           map.addOverlay(marker);
       }
   });
}

// add the listner
var listener = GEvent.addListener(map, "moveend", myNamedFunction);

Kind regards
Natalie

--
Natalie Downe
Senior Client-side Engineer - Clearleft <http://www.clearleft.com>

<http://notes.natbat.net>



More information about the Javascript mailing list