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

  • Home
  • SEARCH
  • 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 6326181
In Process

The Archive Base Latest Questions

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

Can someone clarify the my understanding of variable scope within event handlers? Take a

  • 0

Can someone clarify the my understanding of variable scope within event handlers? Take a look at the code below:

var address = new Array();
address[0] = '1 Smith Street';
address[1] = '2 Smith Street';

for(var rownum=0; rownum<=address.length; rownum++)
{
        if(address[rownum])
                geocoder.geocode( {'address': address[rownum]}, geocodeCallBack);
}


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

        google.maps.event.addListener(marker, 'click', function(){
                var infowindow = new google.maps.InfoWindow({
                content: marker.title
                });
                // how come this event handler knows the marker variable references the marker variable declared about 10 lines above?
                infowindow.open(map, marker);
        });
}

For most people, this code seems straight-forward. It plots two markers on google maps. When you click on the first marker, it shows the address ‘1 Smith Street’. When you click on the second marker, it shows the address ‘2 Smith Street’.

Ok so my question is: how come BOTH markers don’t show ‘2 Smith Street’?

In the past, I’ve looped through arrays of objects and bound event handlers to each object. In the event handler code itself, I would try to re-reference the corresponding object in the array, which is outside of the scope of the event handler. Thus, at the end of page load, the event handler for ALL objects referenced the LAST element in the loop. How come my sample geocode above didn’t experience the same problem?

Forgive me if I’m not articulating the problem well. It is because very confused about the situation. I can’t seem to get my head wrapped around variable scope with event handlers…..if someone can help me clarify, that would be great.

Additional Info/Confusion
Another thing…the variable var marker was instantiated in the scope of geocodeCallBack(). Isn’t the marker destroyed by the time a user triggers google.maps.event.addListener(marker, 'click', function(){}) during run time? In which case, I should get some kind of undefined error?

  • 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:02:31+00:00Added an answer on May 24, 2026 at 5:02 pm

    {'address': address[rownum]} is an object literal. Therefore, its value is determined at the exact time that the statement it is part of is executed, and future changes to address or rownum will not affects the object’s address member.

    You are probably used to seeing this problem with closures (functions that reference variables from a parent scope). That is a different problem entirely, since the body of the function is usually not executed until some time later. Such a function continues to reference the same variables, not simply the same values.

    But in this case, you are not creating any functions at all in your for loop.


    No, the marker variable will still be alive — that’s part of what closures do. If you’re from a C background, this will indeed seem mysterious. The outer function has returned; how could its locals continue to exist!?

    The answer is that these variables get “closed around” by the anonymous function, and the runtime preserves their existence until the anonymous function is no longer referenced.

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

Sidebar

Related Questions

Can someone clarify the significance of the below code. class A { int i
Can someone clarify my thinking on association fixup code in Entity Framework (T4 generated
Can someone please clarify using a SIMPLE user story the full slice of what
Can someone please help to clarify? Also, please mention if there are other representation
Can someone thoroughly explain the last line of the following code: def myMethod(self): #
Can someone clarify for me what the advantages and disadvantages of using MySQLi instead
Can someone clarify - if in my activity, I leave to call an intent
Please can someone clarify something which I should really know by now? If I
Im getting confused with WCF, can someone clarify this for me please. According to
In the context of data-structures synchronization, can someone clarify the difference between lockless 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.