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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:11:44+00:00 2026-05-16T11:11:44+00:00

I have a Google Map, and I am adding event listeners to different things.

  • 0

I have a Google Map, and I am adding event listeners to different things. For instance, I have a Point object, and for this object, I have been adding events as thus:

google.maps.event.addListener(this.marker, 'click', (function(point) {
    return function(event) {
        alert(point.index);
    }})(this));

I have a lot of these events (one for ‘click’, ‘rightclick’, ‘doubleclick’, etc).

When I am adding the event, I create a closure specifically around only the current point. What I am tempted to do however is just:

var point = this;

google.maps.event.addListener(this.marker, 'click', function(event) {
    alert(point.index);
});

I have been avoiding this though because of two reasons.

One is that I’ve seen people that know more about Javascript than I do using “individual” closures, so I figure they must have a good reason.

The second is because (and I don’t know anything about how Javascript is interpreted) I wonder if creating a large closure captures all of the other variables I’m not going to use in my event function (e.g. ‘var color’). Could this cause performance problems?

Thanks for the help!

  • 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-16T11:11:45+00:00Added an answer on May 16, 2026 at 11:11 am

    Both your examples create closures over something called point. In the first one, point is a parameter to the outer anonymous function, and in the second one, point is a local variable. But either way, it is ultimately closed over by an anonymous function.

    Creating an anonymous function accepting one named parameter and then immediately calling it with an argument is just one way of binding a value to a name within a scope.

    (function(x) { 
        // now x has the value
    })(getValue());
    

    Or:

    var x = getValue();
    // now x has the value
    

    The difference is that by using a function, you ensure that the x lives in its own namespace and will not be merged with any other x in the enclosing scope. That is, the value is in maintainability, not speed of execution.

    Which will perform better? The only way to find out is to test on multiple browsers – they all have their own execution engines.

    It seems reasonable to think that the more anonymous functions are created, the more small allocations occur. So your second example, with fewer anonymous functions, may perform better. But that is pure unfounded guesswork without testing in every browser you care about.

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

Sidebar

Related Questions

i have google Maps and when the user touch any point in the map
I have a Google Map involving several different arrays of data, and I'm having
I have a google map using V3 of the API. It has one marker
I have a google map that takes and put coordinates from input fields .
I have a Google Map on the webpage with markers on it that has
I have embedded a Google Map in my website. I want to change its
I have problem with google map Loading and making center in my java script
I have a Google V3 map which uses steetView and some map markers. The
i have a simple google map, showing the driving directions from NY to LA,
i have a multiple marker Google map, it works fine but i want set

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.