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 5941021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:03:05+00:00 2026-05-22T16:03:05+00:00

function initialize(final) { /* …….. */ var address_array = final.split(‘~’); for (var count =

  • 0
function initialize(final) {
        /* ........ */
        var address_array = final.split('~');


        for (var count = 0; count < address_array.length; count++) 
        {
            if (geocoder) {
                geocoder.getLatLng(
                    address_array[count],
                    function (point) {
                        if (!point) {
                            alert(address_array[count] + " not found");
                        }

All I want is the alert to work over here, in the last line.

Suppose address_array has 3 values, i.e address_array.length is 3. But the alert is always showing undefined not found. I am assuming address_array[count] cannot be accessed from this function. However when I try

alert(address_array.length + " not found"); 

it says 3 not found. please help.

can anyone please help me regarding this issue?

function makeTheFunction(array, thisCount) {
return function (point) {
if (!point) {
alert(array[thisCount] + ” not found”);
}

          else {
              var marker = new GMarker(point);
              map.addOverlay(marker);
              GEvent.addListener(marker, "click", function () {
                  marker.openInfoWindowHtml(array[thisCount] + "</b>");
              });
          }
      };
  }

the alert(array[thisCount] + ” not found”); is working fine but it doenst seem to work when it goes into the else section ..marker.openInfoWindowHtml(array[thisCount] + “”);

  • 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-22T16:03:05+00:00Added an answer on May 22, 2026 at 4:03 pm

    This happens because your function is a closure over the count variable, and closures get an enduring reference to the variable, not a copy of its value as of when the function is defined. So all copies of the function see count as of the end of the loop, which is outside the range of the array — hence the undefined.

    What you want to do is create the function such that it closes over something that doesn’t change.

    var address_array = final.split('~');
    
    for (var count = 0; count < address_array.length; count++) 
    {
        if (geocoder) {
            geocoder.getLatLng(
                address_array[count],
                makeTheFunction(address_array, count)
            );
        }
    }
    
    function makeTheFunction(array, thisCount)
    {
        return function(point) {
            if (!point) {
                alert(array[thisCount] + " not found");
            }
        };
    }
    

    We pass both the array reference and the count value as of each iteration of the loop into makeTheFunction, which creates the function as a closure over its arguments and returns that function. Now the function will show the right information, because the information it’s closing over doesn’t change. Note that as written, makeTheFunction can be outside of any other function so as to avoid having the closure close over anything else.

    More about closures: Closures are not complicated

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

Sidebar

Related Questions

Here is the Listener I'm adding - var map; var geocoder; function initialize() {
this is my demo code : <body onload=initialize()> <script> function initialize(){ var d='adddd' $.getScript('other.js',
Why is not possible to initialize a property to a function when you declare
var Helloworld = { onLoad: function() { // initialization code this.initialized = true; },
I'm developing a Firefox extension and have the following code: function initialize() { //
i have a function(initialize) inside the a.as File.i want to access the that Variable
I am using jsdatepicker to display calendar. http://javascriptcalendar.org/javascript-date-picker.php function initialize() { new JsDatePick({ useMode:2,
I have google map on my site(php,apache,mysql). here is code: <script type=text/javascript> function initialize()
function Timer() { this.initialTime = 0; this.timeStart = null; this.getTotalTime = function() { timeEnd
When should I inline a member function and when should I use member initializers?

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.