Going back to the code in your original post, no, there's no reason that
stories[i] would be accessible in your callback function, whether that
function has a name or not. You have only a single "i" variable, and by the
time the geocoder callback is called, "i" is equal to stories.length - which
is outside the range of the stories array.
Neil's reply has the solution for you: Add another function to create a
closure for the current story. It still makes no difference whether your
callback function has a name or not. What makes the difference is the fact
that you've now added a function that gives a closure to hold each
individual stories[i] for later use.
There is one other syntax issue regarding "named functions" (in reality,
with a FunctionDeclaration in ECMAScript), but I don't have time to get into
the details right now. In brief, if you want to use a FunctionDeclaration
like the one in Neil's example, make sure that this function is not inside a
block statement such as a loop or an if statement. It has to be directly
inside another function or in the global scope, not inside a block
statement.
In your code snippet below, *both* versions of the callback function (with
and without the name) are actually FunctionExpressions, so they aren't
subject to that restriction.
-Mike
> From: Arun
>
> 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/t
hread/3...
> >
> b988563f/8a2632e609fba762?lnk=gst&q=anonymous+callback+function#8a2632
> > e609fb
> > 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
-~----------~----~----~----~------~----~------~--~---