Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6328127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:24:11+00:00 2026-05-24T17:24:11+00:00

I’m extending my previous question , because I still don’t fully understand the concept

  • 0

I’m extending my previous question, because I still don’t fully understand the concept of javascript closures. Take a quick look at the following code, which will put two markers on a map. (The code is slightly modified from my previous question).

var map = google.maps.somefunctoinstantiatemap();
var address = new Array();
address[0] = '1 Smith Street';
address[1] = '2 Smith Street';
function onpageload()
{
for(var rownum=0; rownum<address.length; rownum++)
{
    geocoder.geocode({
        'address': address[rownum]
    }, function(results, status) {
        geocodeCallBack(results,status,rownum)
    });
}
}

function geocodeCallBack(results, status, argnum)
{
    var marker = new google.maps.Marker({
        map: map,
        position: results[0].geometry.location,
        title: 'arg: '+argnum+' addr:'+results[0].formatted_address
    });

    google.maps.event.addListener(marker, 'click', function(){
        var infowindow = new google.maps.InfoWindow({
            content: marker.title
        });
        infowindow.open(map, marker);
    });
}

Ok, multiple choice….what is the result when user clicks on both markers?

  1. first marker displays ‘arg: 0 addr: 1 Smith Street’ and second marker displays ‘arg: 1 addr: 2 Smith Street’
  2. first marker displays ‘arg: 0 addr: 2 Smith Street’ and second marker displays ‘arg: 1 addr: 2 Smith Street’
  3. first marker displays ‘arg: 1 addr: 1 Smith Street’ and second marker displays ‘arg: 1 addr: 2 Smith Street’
  4. first marker displays ‘arg: 1 addr: 2 Smith Street’ and second marker displays ‘arg: 1 addr: 2 Smith Street’
  5. first marker displays ‘arg: undefined addr: undefined’ and second marker display ‘arg: undefined addr: undefined’

When I run the code, the answer is 3. But I was expecting the answer to be either 4 or 5. Why is it not 4 and why is it not 5?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T17:24:12+00:00Added an answer on May 24, 2026 at 5:24 pm

    You seem to be expecting the closure to close over something (which it does), but you’re not using anything in particular that it closes over in it (like the loop index). The argnum value you get in your geocodeCallBack comes from Google (because you’ve defined rownum as the third argument to your anonymous function, shadowing the loop counter), it’s nothing to do with your loop counter.

    A few other points about that code:

    Your loop at the top is looping three times, not two. You have it going from 0 to <= results.length. results.length is 2, and so that will loop with rownum values 0, 1, and 2. You mean < results.length. (You’ve fixed this.)

    There’s no purpose to your anonymous function here:

    geocoder.geocode( {'address': address[rownum]}, function(results, status, rownum) {geocodeCallBack(results,status,rownum)});
    

    …since all it does is pass on the arguments it receives. That can and probably should just be:

    geocoder.geocode( {'address': address[rownum]}, geocodeCallBack);
    

    …unless the signature of the function is wrong and you shouldn’t be declaring all of those arguments to it.

    Edit: Based on your comments below, I think you may want this:

    geocoder.geocode( {'address': address[rownum]}, makeCallback(rownum));
    
    function makeCallback(therow) {
        return function(results, status) {
            geocodeCallBack(results, status, therow);
        };
    }
    

    …or in your “real code” as you put it, where you’re actually using that row number as part of a selector:

    geocoder.geocode( {'address': address[rownum]}, makeCallback(rownum));
    
    function makeCallback(therow) {
        return function(results, status) {
            geocodeCallBack(results, status, $('#row-' + therow).val());
        };
    }
    

    The makeCallback function creates a function to be the callback. The function it creates closes over the therow argument passed into makeCallback, which never changes, and so you get therow being 0 for the callback you create on the first loop, and therow being 1 for the callback you create on the second loop.

    In terms of understanding closures, I’ve written up this post which I think you may find helpful: Closures are not complicated It goes into the mechanics of how closures work in some detail. And the title is no lie: They’re not complicated, people tend to tie themselves up in knots because they think they’re complicated, but they’re not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.