[Javascript] Anonymous functions
Terry Riegel
riegel at clearimageonline.com
Wed Dec 5 05:31:19 CST 2007
Hello,
Thanks for the responses. I am still very confused. I have never been
able to get my head around this type of thing. I must be missing
something. How I would do something like the following, and in the
process it may answer my first question. (also is there some
recommended reading I could do on this subject?)
suppose I have something like...
//global context
var a="Hello World";
...
//local context, not global
var a="Goodbye World";
var listener = GEvent.addListener(marker, "click", function()
{ alert(a); });
a="Last Known";
When the event fires what will be in the alert. And how would I pass
information from the local context to the alert?
In case my contrived example doesn't make sense here is the actual
code I am struggling with... I would like the alert to show the value
of i in the local context it is instead showing the last value of i.
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);
var listener = GEvent.addListener(marker, "click", function()
{ alert(i); });
}
Thanks for any help with this.
Terry Riegel
On Dec 4, 2007, at 7:21 PM, Natalie Downe wrote:
> 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>
> _______________________________________________
> Javascript mailing list
> Javascript at lists.evolt.org
> http://lists.evolt.org/mailman/listinfo/javascript
>
More information about the Javascript
mailing list