The original code snipped was:
geocoder.getLatLng(stories[i].city, function plotCity
(latlng) {
I realized it should've been:
geocoder.getLatLng(stories[i].city, function(latlng) {
instead.
If we go by your claim, then stories[i] should be accessible, right ?
But accessible the page still shows "stories[i] is undefined". Where
as
geocoder.getLatLng(stories[i].city, function(latlng, stories[i] { }));
gives error as "missing ) after formal parameters".
How should the parameter be passed in this case ?
If the function is invoked as:
geocoder.getLatLng(stories[i].city, plotCity(latlng, stories[i]));
then the current story object is passed.
-Arun
On Dec 20, 10:03 am, "Michael Geary" <[email protected]> wrote:
> I can assure you - with 100% certainty - that there is no difference
> whatsoever between named functions and anonymous functions with regard to
> scoping rules and closures. They both follow exactly the same rules.
>
> What does matter is where the functions are located in the source code -
> which functions are nested inside which other functions. When one function
> is nested inside another, the inner function can access the variables and
> parameters from the outer function - even if the outer function has already
> returned at the time that the inner function is called. The functions' names
> (or lack of names) don't change this.
>
> In the case of Arun's original code snippet from this thread, there simply
> was no inner function at all to create a closure. It sounds like Arun has
> fixed that, but the fix would work exactly the same whether the inner
> function has a name or not.
>
> -Mike
>
> > From: Rossko
>
> > Or to put it another way,
> > The 'i' based loop runs and kicks off several geocoder requests.
> > Your i appears to be global, can't tell from the snippet.
> > When the loop finishes, i is incremented beyond the end of stories[].
> > Ages later, in javascript terms, a response comes back and
> > the function is called now, processing with the now-invalid i value.
>
> > I'm a bit suspicious of using a named function in this context too ..
> > see this post
>
> http://groups.google.com/group/Google-Maps-API/browse_thread/thread/3...
> b988563f/8a2632e609fba762?lnk=gst&q=anonymous+callback+function#8a2632e609fb
> a762
>
>
>
> > cheers, Ross K
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---